-1

I made a program where I ask the user to input a number out of 0-8, and if it isn't a number a message will appear asking to input a number. If a letter is typed, on the next line it will say "please enter a number."

How do I do this? If it's more than 8 it stays 8 while if it is less than 0 it stays 0.

        if (number >= 8) {
            number = 8;
        } else if (number <= 0) {
            number = 0;
        }

UPDATE:

    System.out.println("Enter The Number you want:<0 - 8> "); 
    number = in.nextInt(); \\ in is the name of scanner 

    try {
      if number = in.nextInt(); 
      if(number >= 8) {
        number = 8;
      } else if (number <= 0) { 
        number = 0;
      }
    } catch (NumberFormatException e) {
      System.out.println("Enter a number");
    }

This didn't work, what am I doing wrong?

4
  • Look into Integer.parseInt(String), and see how it handles receiving something that is not a number. Commented Jan 27, 2014 at 10:53
  • Research.. try something.. if it doesn't work, come back for help with the code you attempted. Commented Jan 27, 2014 at 10:53
  • *** I did try the integer.parseint(String) method and the try catch method. I'll look into those links. Commented Jan 27, 2014 at 10:55
  • Perhaps using a third party library? commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/… Commented Jan 27, 2014 at 11:05

2 Answers 2

2

You can use the Scanner class to read from the System.in and check if the string is a number:

import java.util.Scanner;
import java.util.InputMismatchException;

Scanner sc = new Scanner(System.in);
try {
    int i = sc.nextInt();
} catch (InputMismatchException exc) {
    System.out.println("not a number");
}
Sign up to request clarification or add additional context in comments.

6 Comments

' System.out.println("Enter The Number you want:<0 - 8> "); number = in.nextInt(); \\ in is the name of scanner try { if number = in.nextInt(); if(number >= 8) { number = 8; } else if (number <= 0) { number = 0; } } catch (NumberFormatException e) { System.out.println("Enter a number"); } ' When I tried running it the catch didn't work.
You should catch an InputMismatchException, not a NumberFormatException.
` System.out.println("Enter The Number you want:<0 - 8> "); number = in.nextInt(); try { number = in.nextInt(); if(number >= 8) { number = 8; } else if (number <= 0) { number = 0; } } catch (InputMismatchException e) { System.out.println("Enter a number please"); }` Ran it again and the command line said "Exception in thread "main" java.util.InputMismatchException" etc.
Well, you're calling in.nextInt outside of the try-catch block. I don't even know why do you need two in.nextInt calls.
So I did the code that you provided from scratch again, and it worked thank you! But it didn't stop the program, the program should end right when the user inputs a letter when it should be a number. How do I do that?
|
0

In addition to Nigel Tufnel's answer, if you are not using Scanner (but BufferedReader instead, for instance), you can use the following:

System.out.print("Please enter a number: ");
String numberString = in.readLine();
try {
    int number = Integer.parseInt(numberString);
    System.out.println("You chose number " + number + "!");
} catch (NumberFormatException nfe) {
    System.out.println("Please enter a number");
}

1 Comment

I am using Scanner but thank you for answering.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.