1
public class Main{
    public static void main(String args[]){
    int i = nextInt();
}
public int nextInt(){
    int i=0;
    boolean done=false;
    Scanner scanner = new Scanner(System.in);
    while (!scanner.hasNextInt()){
            scanner.nextLine();
        Printer.println(Printer.PLEASE_NUMBER);
    }
    i=scanner.nextInt();
    scanner.close();
    return i;
}
}

The code above is how I'm trying to force a user to input a int value, but I get the nosuchelement exception, as the scanner.nextLine() reads a NULL. In c++ the software waits for the user to input something. Is there anything I can do to force the program to stop, wait for the user to input something and then make the check?

EDIT: So I'm having problems regardless, if I use scanner outside of Main class, it gives that error...

Vale
  • 1,044
  • 1
  • 10
  • 26

3 Answers3

1

If you want the user to input and the scanner to pick up solely an integer value Scanner provides the method:

int i = scanner.nextInt();

Where i will store the next value entered into the console. It will throw an exception if i is not an integer.

Here is an example: Let's say I want the user to input a number and then I want to spit it back out to the user. Here would be my main method:

public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     System.out.print("Please print your number: ");
     int i = sc.nextInt(); 
     System.out.println("Your Number is: " + i);
}

Now to check whether i is a integer you can use an if statement. However if you want the program to repeat until the user inputs an integer you can use a while loop or a do while loop where the loop's arguments would check if i is an integer.

Hope this is what you were looking for! By the way avoid naming your method nextInt() as the import java.util.Scanner; already has that method name. Don't forget imports as well!

Colin
  • 319
  • 2
  • 14
  • GOtcha on the method name. As you can see I'm already using a while loop, but I get a null read from scanner.nextLine() – Vale Jun 25 '15 at 17:19
  • I'm saying you don't need your method all together. You can just put the Scanner read in a while loop in main by itself and will loop all together. – Colin Jun 25 '15 at 17:26
  • What happens if user enters an invalid integer? (You get an exception. How to handle it?) – DSlomer64 Jun 25 '15 at 17:33
  • Well how I would go about doing this is making a do while loop, and having the scanner continuously get input while some boolean value is true. Then having an if statement that checks if the user enters an invalid integer and if so will not do anything. Otherwise if the value is an acceptable integer it will set your said boolean to false therefore breaking out of the loop. – Colin Jun 25 '15 at 17:37
  • In all truth I haven't worked too closely with this so off the the top of my head I'm not too sure how to handle the exception but I'm mostly sure that you can check if its not an int and that will prevent the exception... I certainly don't know. – Colin Jun 25 '15 at 17:39
1

You can do this:

public static void main(String[] args) {
    System.out.println("" + nextInt());
}

public static int nextInt(){
    int i=0;
    boolean done=false;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a number:");
    while (!scanner.hasNextInt()){
        System.out.println("Please enter a number:");
        scanner.nextLine();
    }
    i = scanner.nextInt();
    scanner.close();
    return i;
}

This will cause the program to stop and wait for input each time the loop is executed. It will keep looping until it has an int in the scanner.

brso05
  • 12,634
  • 1
  • 17
  • 37
  • Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585) at MAIN.Main.nextInt(Messenger.java:22) – Vale Jun 25 '15 at 17:23
  • @Vale works fine for me that means you have something else going on...what does `Printer.println` do? Try copy and pasting exactly what I have using `System.out` and see if it works for you... – brso05 Jun 25 '15 at 17:24
  • @Vale where are you running this `Eclipse` `Netbeans` `Command Prompt`? – brso05 Jun 25 '15 at 17:26
  • Printer is another class, which simply 'System.out.println(s)' where s is the string I send it. I'm seeing I have a problem anyway using scanner: whatever I do I get that exception... Yes I'm using Eclipse Luna 4.4 console – Vale Jun 25 '15 at 17:35
  • @Vale are you using `import java.util.Scanner`? Make sure you have the right `Scanner` class... – brso05 Jun 25 '15 at 17:42
  • I do: import java.util.Scanner; I have no idea what's going on... can I send you the code via mail? Or something? I'd like to see if it's my machine. – Vale Jun 25 '15 at 18:02
  • @Vale the code above works for me which is very similar to your code so it has to be something different with your setup... – brso05 Jun 25 '15 at 18:04
  • Any idea how to understand what the matter is? – Vale Jun 25 '15 at 18:05
  • But what? I'm finding nothing similar to my case – Vale Jun 25 '15 at 18:12
  • @Vale I'm not sure man maybe google your error message you keep getting? `why does scanner always have this error "error message" in java` – brso05 Jun 25 '15 at 18:15
  • I'm Googling like a maniac. Meanwhile I've seen that in the main class these codes work. But I get the same mistake in my class. Have you ever encountered such an event? – Vale Jun 25 '15 at 18:18
0

This works. There surely is a better solution.

EDIT As predicted. Check this,

import java.util.Scanner;

public class NewMain{
  static boolean badNumber;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    do{
      System.out.print("Please print your number: ");
      try{
        int i = sc.nextInt(); 
        System.out.println("Your Number is: " + i);
        badNumber = false;
      }
      catch(Exception e){
        System.out.println("Bad number");
        sc.next();
        badNumber = true;
      }
    }while(badNumber);
  }
}
Community
  • 1
  • 1
DSlomer64
  • 3,998
  • 3
  • 39
  • 82