Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read inputs from console

Tags:

java

i have to read from the following format.

1

12

23

34

So, All inputs are separated by a line.

I have tried the following

   br = new Scanner(System.in);
   num = Integer.parseInt(br.next());
   while(br.hasNextInt()) {
        num = br.nextInt() ; 
        System.out.println(num);
    }

But it is not working as i expected. If i enter first input, it started processing it and prints. it is not waiting for me to enter next line. In C, i can make use of sscanf. but in java i have no idea how to allow user to enter multiline input? plese suggest some ideas

like image 475
Gibbs Avatar asked Apr 26 '26 17:04

Gibbs


1 Answers

Try

br = new Scanner(System.in); while (true) { int num = Integer.parseInt(br.nextLine()); System.out.println(num); }

like image 181
argarevarg Avatar answered Apr 28 '26 06:04

argarevarg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!