Question:
How to declare multiple input statement in a single line in java such as we use %d %d,&a,&b in C language?
Asad Iqbal
2010-09-13 20:09:56 UTC
How to declare multiple input statement in a single line in java such as we use %d %d,&a,&b in C language in a single line in order to get two input,What happend is that when I want to declare two input statement in java I use
a=Interger.parseInt(obj.readLine());
b=Interger.parseInt(obj.readLine());
where a&b are two variables.
how to declare these statements in a single line is there any way to do that please help I'm new in this business
Three answers:
?
2010-09-13 21:23:55 UTC
Hi, Asad!



I think you are not asking how to make two assignments in one line of java code (which, as Jess pointed out, could be done by separating two assignment statements using a semicolon).



Instead, I think we want to allow the user to input two Integers (not "Intergers") on a single console line. This will require multiple lines of Java code, but may be more convenient for the user.



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);

}
?
2016-12-15 08:12:47 UTC
Java Input Statement
?
2010-09-13 21:07:59 UTC
If you end your statements with semicolons, you can put them all on the same line:

a=Interger.parseInt(obj.readLine()); b=Interger.parseInt(obj.readLine());



You can put any statements ending with a semicolon on the same line.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...