0

I am trying to create a program where I use the getInt method to make sure that the user enters a positive number. I have this so far

public class Binary {
  public static void main(String [ ] args) {
   Scanner CONSOLE = new Scanner(System.in); 
   int decimal=getInt(CONSOLE, "Enter a positive integer: ");

   } 

  public static int getInt(Scanner CONSOLE, String prompt) {                      
   System.out.print(prompt);  
   while (!CONSOLE.hasNextInt()) {
    CONSOLE.next();                          
    System.out.println("Not an integer; try again.");                          
    System.out.println(prompt);
   }
   int posInt=CONSOLE.nextInt();
   while (posInt <= 0) {
    System.out.println("Not a positive integer; try again.");
    CONSOLE.next();
    System.out.println(prompt);
   }  
  return CONSOLE.nextInt();
  }  

}    

The issue occurs that when the user does enter a positive number it still disregards the input and asks the user to enter a positive integer again. I guess I'm just not exiting the loop correctly but I'm not sure how.

1
  • 1
    The line return CONSOLE.nextInt(); seems to be prompting the user for yet another integer even if they've already entered one. Commented Apr 17, 2017 at 5:02

2 Answers 2

2

Your problem is return CONSOLE.nextInt();

At the end of your method, you are calling CONSOLE.nextInt() which asks for input once more.

Return posInt, and you'll be fine.

Best of luck, HTH

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

1 Comment

I had tried doing that and it continued to give me the same issue.
0

Like the others said you can return posInt and you should be fine.
But i have some recommendations for your getInt method:

 public static int getInt(Scanner CONSOLE, String prompt) {
    //initialize vars
    boolean valid = false;
    int posInt = 0;
    //while the input is not valid, loop over the evaluation
    while(!valid){
        System.out.print(prompt);
        if (!CONSOLE.hasNextInt()) {
            CONSOLE.next();
            System.out.println("Not an integer; try again.");
            //"continue" stops the loop here and starts it from the beginning
            continue;
        }
        posInt=CONSOLE.nextInt();
        if (posInt > 0) {
            //valid = true will get us out of the loop
            valid = true;

        }else {
            System.out.println("Not a positive integer; try again.");
        }

    }
    return posInt;
}

This code will reevaluate the input from the beginning if the input before was invalid.
In your code if you enter a negative int you will be prompted to reenter a number.
But since you are already in the while (posInt <= 0) loop, it does not recheck if you actually enter a valid input.

The code i provided reevaluates the next input from the beginning until it is found valid.

2 Comments

@ElizabethFlores if this answer worked best for you, please upvote and mark my answer as the best answer so other people with similar problems can find the solution easier. Glad i could help =)
How do I mark it as the best answer? This was my first post ever on this so I'm a newbie

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.