Which method do we use to take user input in Java? In this article, we will discuss How we can take user input in Java, and also we will see How to take input from the Command Line Argument.
Taking input from the user is an important task in any programming language. Here, the input we will take from the console of the application. So, as we know Java follows an OOPs paradigm means, here we have objects and classes. To take input from the user we have a class called Scanner class.
Scanner Class in Java
The Scanner is a Class in Java that helps to get user input from the console window. It is present in java. util package. To use the Scanner class, we need to create the object of it as shown below:
Scanner scanner = new Scanner(System.in);
System.in is an inputStream. It reads whatever we write in the console window. Here, the ‘in’ is an object of InputStream which is define as static in the class System, and then we have different methods to take user input.
Methods of Scanner Class in Java
The Scanner class of Java has lots of methods to take different types of input from the user such as long type, int type, string type and etc.
- nextShort(): This method reads the short value input from the user.
- nextBoolean(): This method reads the boolean value input from the user.
- nextInt(): This method reads the int value input from the user.
- nextLong(): This method reads the long value input from the user.
- next(): This method reads the String value input from the user.
- nextFloat(): This method reads the float value input from the user.
- nextByte(): This method reads the byte value input from the user.
Thus, these are some of the methods that we can use to take user input from the console in Java. Now, let us take a simple example to take different types of user input in Java.
Example to take input from user in Java
package in.codebun; import java.util.Scanner; public class TakeInputFromUser { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter Integer Value"); int a = scanner.nextInt(); System.out.println("Enter String Value"); String str = scanner.next(); System.out.println("Enter Float Value"); float f = scanner.nextFloat(); System.out.println("Entered Values are \n"); System.out.println("Integer Value : "+a); System.out.println("String Value : "+str); System.out.println("Float Value : "+f); scanner.close(); } }
Output:
Enter Integer Value 10 Enter String Value Test Enter Float Value 20.20 Entered Values are Integer Value : 10 String Value : Test Float Value : 20.2
Thus, in this way, we learned How to take user input in Java Programming Language.
How to take input from Command Line Argument in Java?
The Command-Line Argument in Java lets you pass arguments during the execution of the program. As we will use the Command prompt i.e why it is known as Command-Line Argument.
So, to understand this, we will create a class: MyClass.java
class MyClass{ public static void main(String args[]){ System.out.println("First Argument is "+args[0]); System.out.println("Second Argument is "+args[1]); System.out.println("Third Argument is "+args[2]); } }
Now, we will use the command prompt to compile and run. Go to start > type cmd > right-click on it and run as administrator > Now go to the path where your Java program is written:
C:\Windows\system32>cd C:\Users\admine\Desktop\Java
Compile the Program
C:\Users\admine\Desktop\Java>javac MyClass.java
Now, pass the argument from the command line as shown below
C:\Users\admine\Desktop\Java>java MyClass Old Is Gold
Now, press enters key, and you will notice that the arguments are passed to args[] as shown below. Here, the args is the array of strings that stores all the arguments.
Can we have any other name for args[]?
Yes, definitely. We can have any name/alphabet because it is just a variable we use to accept all the parameters from the command line.
Is there any other way we can write String args[]?
Yes, in J2SE 5.0 Varargs or variadic function was introduced. We have to use a series of dots(…) as shown below. It shows a function with an unlimited number of arguments.
class MyClass{ public static void main(String... a){ for (String a1 : a) { System.out.println(a1); } } }
Thus, this is how we can take input from the Command Line argument in Java.