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 is a condition in java?

712


When do you call copy constructor?

727


How does a for loop work java?

776


Which package has light weight components?

773


Can we create more than one object singleton class?

808


Is there any case when finally will not be executed?

721


Can we write any code after throw statement?

816


What is java thread dump, how can we get java thread dump of a program?

772


How do you call a reference in java?

699


Explain the difference between abstract classes and interfaces in java?

837


What is r in java?

783


What is percentage in java?

764


List any five features of java?

780


What is null mean in java?

847


How to reverse a string in java?

760