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 are the two types of exceptions in java? Which are the differences between them?

516


What does java ide mean?

642


Can a vector contain heterogenous objects?

602


Can we make the abstract methods static in java?

587


Is 0 a prime number?

572






What string is utf8?

547


What is core java used for?

518


How do you reverse a string in java?

583


which one is more efficient int x; 1. if(x==null) 2.if(null==x) state which one either 1 or 2?

9239


How do you read and print a string in java?

527


What are different access specifiers in java?

567


How do you use compareto in java?

561


Explain what are final variable in java?

576


Why arraylist is not synchronized in java example?

471


Explain about data types?

576