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...

1
  • Please post full program! Commented Jun 25, 2015 at 17:08

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

5 Comments

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()
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.
What happens if user enters an invalid integer? (You get an exception. How to handle it?)
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.
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.
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.

11 Comments

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 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...
@Vale where are you running this Eclipse Netbeans Command Prompt?
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 are you using import java.util.Scanner? Make sure you have the right Scanner class...
|
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);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.