3

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

8 Answers 8

1

Try

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

Sign up to request clarification or add additional context in comments.

2 Comments

On second thought, the true should be replaced, but I need more context to improve it further
this doesn't terminate in the end. it will throw exception
1

You must check for next available input and then get the input

br = new Scanner(System.in); //num = Integer.parseInt(br.next());//remove this line while(br.hasNextInt()) {//if number is avaialable num = br.nextInt(); //get that number System.out.println(num); } 

Below is sample code:

import java.util.Scanner; class Ideone { public static void main (String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()) System.out.printf("input was: %d\n",sc.nextInt()); sc.close(); } } 

Comments

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

you need to add br.nextLine(); to start to read next line.

3 Comments

No, It doesn't work. It works like "input [prints] next_input [print]". I expect input next_input [print inpur next_input]
First i have to get all the numbers and the n i want to process
@user3801433 I have resolved your code only. It will print as like your code. I didnt change anything. I just added nextLine(). It moved pointer to next line in scanner
0

It's not entirely obvious what you require , but this code ( below ) will allow you to read and store multiple lines of input , and then print them all when you're finished .

systemInScanner = new Scanner ( System.in ) ; ArrayList < String > arrayListOfStrings = new ArrayList < > ( ) ; while ( systemInScanner . hasNextLine ( ) ) { arrayListOfStrings . add ( systemInScanner . NextLine ( ) ) ; } for ( String line : input ) { System . out . println ( line ) ; } 

Comments

0

if you willing to read line by line . This code read line by line and exits when you input a empty line

br = new Scanner(System.in); int num=0; String input; input = br.nextLine(); while(input.length()!=0) { num = Integer.parseInt(input); System.out.println(num); input = br.nextLine(); } 

Comments

0

Your question isn't entirely clear; however if you're trying to get the user input- then print it later, you can save the inputs to an ArrayList.

import java.util.ArrayList; import java.util.Scanner; public class tu { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter numbers, when you're done, enter a letter."); char firstLetter = '*'; ArrayList<String> al = new ArrayList<>(); while(!Character.isLetter(firstLetter)) { String input = ""; boolean nullInput = false; do{ if(nullInput) System.out.println("Error; you can't enter an empty string."); nullInput = false; input = in.nextLine(); if(input.length() == 0) nullInput = true; }while(nullInput); firstLetter = input.charAt(0); al.add(input); } al.remove(al.size()-1); for(int i = 0; i < al.size(); i++) { System.out.println(); //creates newline in console System.out.println(al.get(i)); } } } 

Comments

0
Scanner scanner = new.scanner(System.in); String abc = scanner.next(); int a = scanner.nextInt(); scanner.close(); 

Comments

0
 br = new BufferedReader(new InputStreamReader(System.in)); int ch; while ((ch = br.read()) != -1) { if (ch == 10) { System.out.println(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.