How to declare multiple input statement in a single line in java???
example:
-
taking input of three integers: 2 3 4 (taking input in a single line)
-
taking input of three integers: 2
3
4
i am talking about example 1.
How to declare multiple input statement in a single line in java???
example:
taking input of three integers: 2 3 4 (taking input in a single line)
taking input of three integers: 2
3
4
i am talking about example 1.
To do this, we could read in the user’s input String by wrapping an InputStreamReader object in a BufferedReader object.
Then, we use the readLine() method of the BufferedReader to read the input String – say, two integers separated by a space character. These can be parsed into two separate Strings using the String.split() method, and then their values may be assigned to a and b, using the parseInt method as you suggest above.
(We would likely want to verify the user input as well to ensure it is in the desired format, but that issue hasn’t been addressed here.)
First, remember to import java.io.* for the Reader objects. The readLine() method throws an IOException, so the Main method can go like this:
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(
new InputStreamReader(
System.in));
String[] input = new String[2];
int a;
int b;
System.out.print("Please enter two integers: ");
input = in.readLine().split(" ");
a = Integer.parseInt(input[0]);
b = Integer.parseInt(input[1]);
System.out.println("You input: " + a + " and " + b);
}
------------- ADDITIONAL INFO --------------
I forgot about the Scanner object. This can make things a lot simpler. Instead of importing the Reader objects, we import java.util.Scanner
The Scanner object can parse user input directly, so we don’t have to split Strings or use parseInt. Also, we don’t necessarily need to worry about catching an IOException. And, the user may input both integers on the same line, or even on different lines, as desired.
Using the Scanner object, our much simpler main function can go like this:
public static void main(String[] args)
{
System.out.print("Please enter two integers: ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("You input: " + a + " and " + b);
}
source :- Yahoo Answers
@dpnkr: to take input:
using BufferedReader :
String s[]=br.readLine().split(" "); //s[0] contains 1st number, s1 second number and so on.
//3 variables:
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s1);
c=Integer.parseInt(s[2]);
//For an array:
//n=number of elements
loop i=0 to n
a[i]=Integer.parseInt(s[i]);
FAST I/O:
But Fast I/O works better link to the fast I/O (contains for c,c++ and java)
|==HAPPY NEW YEAR==|
is there anyway to give inputs by pressing enter not by space to declare in a single line…??
@dpnkr: i dont think there is any inbuilt method to in java but what you need can be done using Fast I/O
thanks
and Happy New Year
do parsing after taking input from BufferedReader into string .
Use IntegerParseint for this .
But you cannot input in one line, as in case of Scanner it will point to next line to enter the next integer, but yes your above answer for bufferedreader is right as in that case we have to enter integers as a string