+ 3
Is there any other way to take input from user?
4 Answers
+ 3
You have the 'Scanner' and the 'JOptionPane' Classes
+ 3
I know 4 ways out of which 1 is outdated.
Outdated one is:-
int a=System.in.read();
The other 3 ways are:-
1. Scanner class.
2. BufferedReader class
3. This method can only be applied when we are compiling and running the program through CMD.
For example:- If the name of the file is sum.java in which there is a program that inputs 2 numbers and print their sum and the file is stored in D drive, then the codes of the program are:-
class sum
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.print("The sum is "+c);
/*You can also apply try/catch.*/
}
}
And the statements in CMD are as follows.
D:/javac sum.java
D:/java sum 2 3
The sum is 5
+ 1
Yes you could also use JOptionPane
0
Agree to @Sarthak. You can also use the DataInputStream class. It's similar to the Scanner class.