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


Please Help Members By Posting Answers For Below Questions

What do you understand by an io stream?

575


What is the difference between exception and error in java?

486


what is meant by abstract class?

646


What is broken and continue statement?

527


What is mean by encoding?

625






What is a class reference?

550


What are benefits of java?

552


When can we say that threads are not lightweight process in java?

588


Is finalize() similar to a destructor?

546


How can I debug the Java security exceptions and AccessControlExceptions?

627


What about anonymous inner classes in java?

559


What is the loop in java?

566


What is ide with example?

542


What is an empirical question?

536


What is thread life cycle in java?

578