0

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?

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • Look into [Integer.parseInt(String)](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29), and see how it handles receiving something that is not a number. – Henrik Aasted Sørensen Jan 27 '14 at 10:53
  • Research.. try something.. if it doesn't work, come back for help with the code you attempted. – Paul Samsotha Jan 27 '14 at 10:53
  • *** I did try the integer.parseint(String) method and the try catch method. I'll look into those links. – user3240125 Jan 27 '14 at 10:55
  • Perhaps using a third party library? http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/NumberUtils.html#isNumber(java.lang.String) – Ben Dale Jan 27 '14 at 11:05

2 Answers2

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");
}
Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
  • ' 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. – user3240125 Jan 27 '14 at 11:08
  • You should catch an `InputMismatchException`, not a `NumberFormatException`. – Nigel Tufnel Jan 27 '14 at 11:14
  • ` 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. – user3240125 Jan 27 '14 at 11:32
  • 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. – Nigel Tufnel Jan 27 '14 at 16:16
  • 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? – user3240125 Jan 27 '14 at 17:06
  • Mate, you should read a couple of Java tutorials. Maybe read a book. – Nigel Tufnel Jan 27 '14 at 17:36
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");
}
Ben Barkay
  • 5,473
  • 2
  • 20
  • 29