In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what are the knowledge points about Java input data, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
In the process of Java program development, it is common to get input values from the keyboard. C language provides scanf () function, C++ provides cin () to get keyboard input values. So what is the solution for Java?
Method 1: receive a character from the console and print it out
Package pkg2020 South China Tiger; import java.io.*;/** @ author yl * / public class JavaInput {public static void main (String [] args) throws IOException {System.out.println ("Enter a Char:"); char I = (char) System.in.read (); System.out.println ("Your char is:" + I);}}
Although this approach allows you to get input characters from the keyboard, System.out.read () can only fetch one character, and the type of variable you can get can only be char. When entering numbers, you also need to convert the type.
Method 2: receive a string from the console and print it out. Need to embrace the BufferedReader class and InputStreamReader class.
Package pkg2020 South China Tiger; import java.io.*;/** @ author yl * / public class JavaInput02 {public static void main (String [] args) throws IOException {BufferedReader br=new BufferedReader (new InputStreamReader (System.in)); String str=null; System.out.println ("Enter your value:"); str=br.readLine (); System.out.println ("Your value is:" + str);}}
Method 3: the simplest and most powerful is the Scanner class
Package pkg2020 South China Tiger; import java.util.Scanner;/** @ author yl * / public class JavaInput03 {public static void main (String [] args) {Scanner sc=new Scanner (System.in); System.out.println ("Input your name:"); String name=sc.nextLine (); System.out.println ("Input your age:"); int age=sc.nextInt (); System.out.println ("Input your salary:"); double salary=sc.nextDouble (); System.out.println ("Your information is follow as:") System.out.println ("Name:" + name+ "\ n" + "Age:" + age+ "\ n" + "Salary:" + salary);}}
Finally, summarize the differences between next () and nextLine ():
In Java, the next () method does not accept spaces. Before receiving valid data, all inputs such as spaces or tab keys are ignored, and if there is valid data, these keys exit. NextLine () can receive spaces or the tab key, and its input should end with the enter key.
Which knowledge points about Java input data are shared here, I hope the above content can help you to a certain extent, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.