0
  • I have a main function in which I use Scanner to read an integer from the console.
  • Inside this main function, we can access another function which also uses the Scanner to read an integer.
  • The program "swings" between these two functions many times.

The problem is that the Java.util.Scanner throws an exception.
Is there any way to overcome this?

import java.util.Scanner;

public class dummy {

    public static void main(String[] args) {
        int buy;  
        Scanner sc = new Scanner(System.in);

        buy = sc.nextInt();
        user = dummy2();

        sc.close();
    }

    private static boolean dummy2() {
        Scanner sc1 = new Scanner(System.in);

        // Code...

        sc1.close();
    }
}


      
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
user3683555
  • 115
  • 2
  • 11

3 Answers3

2

Use the same Scanner object.

import java.util.Scanner;

public class dummy {
    private static final Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int buy;  

        buy = sc.nextInt();
        user = dummy2();

        // Do more stuff with the same scanner

        sc.close();
    }

    private static boolean dummy2() {
        // Scan stuff
    }
}
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Jakkra
  • 641
  • 8
  • 25
  • @user3683555: likely because you're declaring it inside of the main method and not before the method. You would need to declare it static and in the class. – Hovercraft Full Of Eels Jul 14 '14 at 01:21
  • You're shadowing `sc` in `main`. Further, since we're always going to pull from `System.in`, why not just declare and instantiate it as a field? – Makoto Jul 14 '14 at 01:21
  • Should work now, I forgot to delete "Scanner" in Scanner sc = new... in main. – Jakkra Jul 14 '14 at 01:21
  • illegal modifier for parameter sc ; only final is permitted --> this is the error i got after using private static in front of scanner. – user3683555 Jul 14 '14 at 01:40
2

First of all, it would make the question much easier to answer if you gave more information, such as the exception and its message, and maybe source code.

If the exception is a NoSuchElementException, the direct problem is that the function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid.

If the exception is InputMismatchException, then the input is not an int.

If the exception is IllegalStateException, then the scanner has been closed, this could happen is the function and the main method are using the scanner, and one closes it.

However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input.

yesennes
  • 1,147
  • 1
  • 10
  • 19
  • This is a very useful answer. I get Nosuch element exception as problem is function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid. Yes, you are correct. But, i need to find a way to get 2 inputs from the user at 2 different functions. – user3683555 Jul 14 '14 at 01:25
  • However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input ---> this sums it up. I will tell u if it worked – user3683555 Jul 14 '14 at 01:26
-1

I would suggest something like that:

import java.util.Scanner;

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

    public static void main(String[] args) {

        int buy;  

        buy = sc.nextInt();
        user = dummy2();
        sc.close();

    }

    static boolean dummy2(){
        //lets scan a string.
        sc.nextLine();
    }
}

Reusable objects! Isn't that nice?

g.carvalho97
  • 332
  • 1
  • 9