0

I am in a Java course that's starting off with object oriented programming. It isn't a beginning course. The problem is I am not familiar with Java so I am reading a beginner book on my own to get caught up. Due to that, I am having a few issues with my code. I am seeking to validate the user input from a menu and I want the user to only enter a positive integer, and to print out a statement when a string character or a negative integer is entered. So far I have

import java.util.Scanner;


public class Assignment1 
{
    public static void main(String[] args) 
    {
        int choice;

        Scanner input = new Scanner(System.in);
        System.out.println("Menu\n----");
        System.out.println("1) Bar Code to Zip Code");
        System.out.println("2) Zip Code to Bar Code");
        System.out.println("3) Exit\n");
        System.out.print("4) Choice: ");

        while (!input.hasNextInt())
        { 
            input.next();
            System.out.print("Error.  Enter 1, 2, or 3: ");
        }
        choice = input.nextInt();

    }
}

I got it to reject string input, but do not know how to get it to also reject negative values as well. Any. help would be appreciate

Tay
  • 101
  • 1
  • 5

0 Answers0