1

First, I would like to say that this is for a homework assignment and I'm only looking for suggestions. Not answers! I am very determined to learn and be good at programming, and that doesn't come from someone else doing your work. Point me in the correct direction and it will be greatly appreciated!

Please know that I have searched the internet for a solution but haven't found one that fits my needs. I'm unable to use any advanced methods.

The program is to allow the user to input a start and end number for a range. The start number must be divisible by ten and the end must be divisible by ten and not be the same as the starting number. The user is limited to use only numbers between 0 - 1000 and not be allowed to enter any other characters on the keyboard. So if they hit "a" or "1200" the program should loop back until a valid entry has been entered.

Currently I'm stuck on only allowing an integer to be input. The particular part of my code is posted below:

while(errorLoop != true){
        System.out.println("Enter the Starting Number of the Range (ex. 10,70,100)");
        startNum = kb.nextInt();
        System.out.println("Enter the Ending Number of the Range (ex. 10,70,100)");
        endNum = kb.nextInt();
        if(startNum % 10 == 0 && endNum % 10 == 0){
            errorLoop = true;
        }else{
            errorLoop = false;
            System.out.println("Start and End of range must be divisible by 10\n");
            System.out.println("Please try again (ex. 10,70,100)\n");
        }
    }

I have only posted the part of the code that pertains to the question. If you must know the point of the program, the range of numbers will be sorted by prime numbers and output to a table looking format where each row ends with a number that is divisible by ten. The non-prime numbers will be printed as a "-".

Ex. 71 - 73 - - - - - 79 | 80 \n and it goes on for however big the range is.

billabrian6
  • 321
  • 3
  • 7
  • 21
  • Hint: what happens when someone enters a non-integer? How can you handle that thing that happens? – matt b Sep 26 '12 at 20:56
  • For future reference, homework questions should be tagged with the "homework" tag. I have taken the liberty of adding it for you. – Jesse Webb Sep 26 '12 at 20:56
  • 4
    @JesseWebb The homework tag just got deprecated so adding it was not the best idea. See [here](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated?cb=1) – Robin Sep 26 '12 at 20:57
  • @Robin - THX for the heads up! I was unaware of that. – Jesse Webb Sep 26 '12 at 21:03
  • @JesseWebb Thanks for the initiative in trying to tag my post. I had planned on tagging it with homework, but before I posted I had seen on the homepage that the tag being deprecated. – billabrian6 Sep 26 '12 at 22:00
  • @Robin Thanks for the clarification! – billabrian6 Sep 26 '12 at 22:01

4 Answers4

3

I would advise you to use nextLine() instead of nextInt(). Then you can first make sure that it is parseable as an Integer (check the Integer JavaDoc page), and then that the number meets your requirements.

EDIT
To handle the case where the input isn't numeric, you can go in several directions. I prefer checking the input before the actual parse call with a regex. A String containing only digits would match "^\\d+$" (check out this link for a great regex tutorial), and there is a handy method in the String API.

Keppil
  • 43,696
  • 7
  • 86
  • 111
  • Thanks for the suggestion! I would post code, but I can't seem to make it work in the comment section =/. Anyways, I added another variable for num1 = kb.nextLine(); and changed startNum = Integer.parseInt(num1). The program still works but it still blows up if I type in "a" when prompted to enter an integer. – billabrian6 Sep 26 '12 at 21:52
  • Sorry for all of the unnecessary edits, bad habit of hitting the enter key. I don't use this site often. I'm doing my best to learn to use it. – billabrian6 Sep 26 '12 at 21:55
  • @billabrian6: Added some more info. – Keppil Sep 27 '12 at 04:59
0

You could always parse your input with a Regex to ensure they're numbers:

int number = Integer.parseInt(kb.nextLine().replaceAll(”[^\\d]“, “”));

Input:

1blahblah2moretext3

Produces number:

 123
MrLore
  • 3,654
  • 2
  • 24
  • 36
0

write a method that checks if the entered number is numeric.

  boolean method(String num) {
       boolean retValue;
      try {
        // check out the Integer API for the methods related to parse an int
          retvalue=true;
        }
      catch(ParseException ex) {
         retvalue=false;
      } 
     return retvalue;    
   } 
PermGenError
  • 43,905
  • 8
  • 81
  • 104
0

Try using the below exceptions in your program, make sure you use InputMismatchException and import it.

   try {
        System.out.println("Enter the value of a"); 

        int a = scanner.nextInt();


        System.out.println("Enter the value of b"); 
        int b = scanner.nextInt();
        int c = a+b;
        System.out.println("The answer is " +c);
        } 


    catch (InputMismatchException exception) 
    //Add import java.util.InputMismatchException; at the top
        {
        System.out.println("Error - Enter a integer");
        }
akshayhallur
  • 71
  • 1
  • 6