Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop reading multiple lines from stdin using Scanner?

I'm working on an JAVA assignment should process multiple lines of input. The instructions read "Input is read from stdin."

An example of sample input is given:

one 1
two 2
three 3

I don't understand what the above sample input "read from stdin" means.

Here's a test program I wrote that isolates my confusion:

import java.io.*;
import java.util.Scanner;

class Test
{
public static void main(String[] args)
{   
    Scanner stdin = new Scanner(System.in);
    while(stdin.hasNextLine())
    {
        String line = stdin.nextLine();
        String[] tokens = line.split(" ");
        System.out.println(Integer.parseInt(tokens[1]));
    }
}

When I run this program in the console, it waits for my input and each time I input a line it echos it back as I would expect. So I thought perhaps the sample input above would be achieved by entering each of the 3 lines in this fashion. However, there seems to be no way to end the process. After I enter the 3 lines, how do I terminate the input? I tried just pressing enter twice, but that seems to read as a line consisting of only the newline character, which causes an error because the line doesn't fit the 2 token format it expects.

Here's what the console interaction looks like:

javac Test.java
java Test
one 1
1
two 2
2
three 3
3

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Test.main(Test.java:13)

I'd appreciate any help in pointing out the gap in my understanding.

like image 475
dmoench Avatar asked Jan 19 '13 04:01

dmoench


People also ask

Is StdIn a Scanner?

StdIn and Scanner are both designed to parse tokens and convert them to primitive types and strings. The main differences are summarized below: StdIn is a set of static methods and reads reads input from only standard input. It is suitable for use before a programmer knows about objects.

How do you input multiple lines in Java?

For example, if want to take input a string or multiple string, we use naxtLine() method. It is only a way to take multiple string input in Java using the nextLine() method of the Scanner class.


2 Answers

You could try asking for empty inputs

import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {   
        String line;
        Scanner stdin = new Scanner(System.in);
        while(stdin.hasNextLine() && !( line = stdin.nextLine() ).equals( "" ))
        {
            String[] tokens = line.split(" ");
            System.out.println(Integer.parseInt(tokens[1]));
        }
        stdin.close();
    }
}
  • Your code is almost completed. All that you have to do is to exit the while loop. In this code sample I added a condition to it that first sets the read input value to line and secondly checks the returned String if it is empty; if so the second condition of the while loop returns false and let it stop.
  • The array index out of bounds exception you will only get when you're not entering a minimum of two values, delimitted by whitespace. If you wouldn't try to get the second value >token[1]< by a static index you could avoid this error.
  • When you're using readers, keep in mind to close after using them.
  • Last but not least - have you tried the usual Ctrl+C hotkey to terminate processes in consoles?

Good luck!

like image 113
marwils Avatar answered Nov 16 '22 01:11

marwils


You could also put your values in a file e.g. input.txt and do:

java Test < input.txt
like image 44
denmojo Avatar answered Nov 16 '22 01:11

denmojo