Input and output are the two main fundamental aspects of any programming language. The keyboard and screen are the basic devices for input and output respectively. User input is very important to make an application interactive. By collecting input, a Java program can customize its output, perform specific operations, or adjust its functionality to meet the needs of different users. Different Java packages contain other classes to get input from the user. This article discusses how to get user input using Java.
In a Java program, there are three ways to obtain input from the user. They are as follows-
Scanner class is used to get user input in Java. It is one of the most common and simplest ways to get user input. It is part of the java.util package. We can use the Scanner class to get input of primitive types such as int, char, float, double, etc. It's easier to read, making it the most common choice.
Scanner scn=new Scanner(System.in);
Importing the java.util package is very important for using the Scanner class. In the above statement, we create a Scanner class constructor and then pass the System.in parameter to enable it to read from the standard input stream.
nextInt() | is translated as:is used to read and return the next integer value. |
nextFloat() | Used to read and return the next floating point value |
nextDouble() | Used to read and return the next double value. |
Next() | It is used to read and return the value of the next word as a string. |
nextLine() | It is used to read and return the next multiple word value as a string |
nextLong() | It is used to read and return the next long integer value. |
nextShort() | It is used to read and return the next short integer value. |
nextByte() | is used to read and return the next byte value. |
nextBoolean() | It is used to read and return the next boolean value. |
以下是使用 Java 中的 Scanner 类获取用户输入的示例。
//import the java.util package import java.util.*; public class Main { public static void main(String args[]) { // to take the input, use the Scanner Class Scanner scn = new Scanner(System.in); System.out.println("Enter your roll no"); int roll_no = scn.nextInt(); //integer input System.out.println("Enter your name"); String name= scn.next(); //String input System.out.println("Enter your marks"); double marks = scn.nextDouble(); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); } }
以下是上述代码的输出
Enter your roll no 1 Enter your name Sita Enter your marks 99 Your roll no is- 1 your name is- Sita and marks- 99.0
在上面的代码中,我们在Java中创建了一个Scanner类。我们以int、String和double的形式获取了三个不同的用户输入。获取输入后,我们将它们打印出来
易于实施。
简单的方法。
速度较慢。
它不是同步的,因此不适合多线程。
BufferedReader是Java中另一个用于从用户获取输入的类。它在JDK 1.0中引入,并且是从用户获取输入的基本方法。它位于java.io包中。
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
这意味着我们创建了一个BufferedReader对象。InputStreamReader类用于将输入的字节流转换为BufferedReader可以理解的字符流。System.in是标准输入,默认为键盘。
readLine()方法用于读取一行并将其存储为字符串。如果字符串需要转换为其他数据类型,必须显式进行类型转换。
以下是使用 Java 中的 BufferedReader 类获取用户输入的示例。
//import the java.io package import java.io.*; public class Main { public static void main(String args[]) throws IOException { // to take the input, use the BufferedReader Class BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your roll no"); int roll_no = Integer.parseInt(br.readLine()); //integer input System.out.println("Enter your name"); String name= br.readLine(); //String input System.out.println("Enter your marks"); double marks = Double.parseDouble(br.readLine()); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); } }
以下是上述代码的输出 -
Enter your roll no 1 Enter your name Sita Enter your marks 94 Your roll no is- 1 your name is- Sita and marks- 94.0
创建了一个名为"br"的BufferedReader对象。它使用InputStreamReader类从用户那里获取输入,该类将输入的字节转换为字符。我们已经从用户那里获取了输入,但它的默认值是一个字符串。然后将其强制转换为所需的数据类型
更快的性能
可以与多个线程一起使用。
只能接受字符串输入。稍后必须将它们转换为所需的数据类型。
可能需要导入多个库才能使用 BufferedReader 类。
使用 Console 类是通过命令行获取用户输入的另一种方法。这是在 JDK 1.5 中引入的,并且存在于 java.io 包中。 Console 类对于从控制台读取密码非常有用。此外,与其他方法相比,它的实例化很简单。 java.io.Console 库对于 Console 类至关重要。
Console c = System.console();
以下是使用 Java 中的 Console 类获取用户输入的示例。
//import the java.io package import java.io.*; public class Main { public static void main(String args[]) throws IOException { System.out.println("Enter your roll no"); int roll_no = Integer.parseInt(System.console().readLine()); //integer input System.out.println("Enter your name"); String name = System.console().readLine(); System.out.println("Enter your marks"); double marks = Double.parseDouble(System.console().readLine()); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); } }
以下是上述代码的输出 -
Enter your roll no 1 Enter your name Sita Enter your marks 96 Your roll no is- 1 your name is- Sita and marks- 96.0
在上面的代码中,我们直接使用System.console()函数读取了用户输入。
这个类使用了同步方法
不能在像IDE这样的环境中使用,其中所有输入不是一次性给出的。
因此,我们了解了如何在Java中从用户那里获取输入。根据个人偏好,我们可以使用其中任何一种。
The above is the detailed content of How to accept user input in Java?. For more information, please follow other related articles on the PHP Chinese website!