how can i take the inputs from users in java program?
Answer Posted / mohammad faisal
There are two methods to input a text in java.
The first one is through command line arguments.
i.e.,At the run-time we have to pass all the inputs.
But there is a problem to the user of the program because
he is unable to get the order of the inputs.
Therefore a programmer must have to prefer the readLine
method.
The readLine method can be implemented like:
//1st method
import java.io.*;
class Program
{
public static void main(String args[])
{
String str="";
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter your name: ");
str=br.readLine();
System.out.print("You have entered: "+str);
}
}
//2nd method
import java.util.*;
class Program
{
public static void main(String args[])
{
String str="";
Scanner s=new Scanner(System.in);
//works in jdk 5&above
System.out.print("Enter your name: ");
str=s.next();
System.out.print("You entered: "+str);
}
}
| Is This Answer Correct ? | 18 Yes | 0 No |
Post New Answer View All Answers
What is mean by collections in java?
Difference between overriding and overloading in java?
What are different access specifiers in java?
Can a java program have 2 main methods?
Which is better arraylist or vector?
Which collection allows duplicate values in java?
What method is used to specify a container's layout in java programming?
What is return used for in java?
How do you replace all in word?
What is the requirement of thread in java?
Is stringwriter thread safe?
What's the default access specifier for variables and methods of a class?
Is it possible to compare various strings with the help of == operator?
Which of the following is not an isolation level in the JDBC
What are the three parts of a lambda expression?