How to read inputs from the console

I need 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 doesn't work as I expected. If I enter the first input, it starts processing it and prints. it doesn't wait for me to enter the next line. In C, I can use sscanf. but in java I have no idea how to allow the user to enter multiline input? plese suggest some ideas

+3


source to share


8 answers


You have to check the 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 a 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();
    }
}

      

+1


source


Try



br = new Scanner(System.in);

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


+1


source


   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 reading the next line.

0


source


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

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

      

0


source


if you want to read line by line. This code is read as a line and is output when you enter an 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();
        }

      

0


source


Your question is not entirely clear; however, if you are trying to get user input and then print it out later, you can store the inputs in 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));

        }

    }


}

      

0


source


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

      

0


source


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

      

0


source







All Articles