3

I'm learning how to use Scanner in Java, I want to read from stdin an integer and a string, so I write the following code:

import java.util.Scanner;

public class TryScanner {
    public void readInt() {
        Scanner in = new Scanner(System.in);
        System.out.println("Please type in an integer");
        int length = in.nextInt();
        System.out.println("Integer you type is: " + length);
        in.close();

    }
    public void readString() {
        Scanner in = new Scanner(System.in);
        System.out.println("Please type in a string");
        String s = in.nextLine();
        System.out.println("String you type is: " + s);
        in.close();

    }
    public static void main(String[] args) {
        TryScanner instance = new TryScanner();

        instance.readInt();
        // throw exception when called after readInt()
        instance.readString();
    }
}

My readInt and readString function work well separately, however, when I call them sequentially, only the first function works, an exception is thrown for the second:

Please type in an integer
3
Integer you type is: 3
Please type in a string
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1597)
    at helloworld.Sort.readString(Sort.java:17)
    at helloworld.Sort.main(Sort.java:27)

Can somebody explain where I did wrong?

Bamqf
  • 2,792
  • 7
  • 26
  • 43

2 Answers2

4

Remove the call to

in.close();

when you close in, you also close the System.in global (and it doesn't re-open).

public void readInt() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please type in an integer");
    int length = in.nextInt();
    System.out.println("Integer you type is: " + length);
    // in.close();
}
public void readString() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please type in a string");
    String s = in.nextLine();
    System.out.println("String you type is: " + s);
    // in.close();
}

Also, you might consider passing the Scanner to the method(s).

public static int readInt(Scanner in) {
    System.out.println("Please type in an integer");
    return in.nextInt();
}
public static String readString(Scanner in) {
    System.out.println("Please type in a string");
    return in.nextLine();
}
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Integer you type is: " + readInt(in));
    System.out.println("String you type is: " + readString(in));
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • Thank you for your answer, I have one more question. If I just delete `in.close()` within my two functions, my IDE(Eclipse) will complain `Resource leak: 'in' is never closed`, however if I try your version that passes Scanner my IDE won't complain about the absence of `in.close()`. Why? – Bamqf Jul 09 '15 at 04:16
  • The first is a counterproductive warning (in this case), `System.in` is a global. By passing the resource into the method, only one instance is created. I would have expected that `main` would now complain about the possible resource-leak. In `main` it's probably safe to `close` `in` (before `exit`), but I'd prefer using a [`try-with-resources`](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). – Elliott Frisch Jul 09 '15 at 04:17
  • Another lesson learnt. Thanks. – Bamqf Jul 09 '15 at 04:21
0

You can use the below code as well

import java.util.Scanner;

public class TryScanner {
    Scanner in = new Scanner(System.in);

    public void readInt() {

        System.out.println("Please type in an integer");
        int length = in.nextInt();
        System.out.println("Integer you type is: " + length);

    }

    public void readString() {
        System.out.println("Please type in a string");
        String s = in.nextLine();
        System.out.println("String you type is: " + s);

    }

    public static void main(String[] args) {
        TryScanner instance = new TryScanner();

        instance.readInt();
        instance.readString();
        instance.in.close();
    }
}
Kalyan Chavali
  • 1,220
  • 7
  • 24