user3044041
user3044041

Reputation: 71

Testings to see if a string has integers java

I am trying to test if a string has integers in it. I know I can do the:

Integer.parseInt(String);

but I am trying to replicate this menu.

How am I able to test the string to see if it is a string of characters like six or if it is an actual integer.

EDIT: Completed using the try and catch method:

Fixed code:

private static void decadeMenu(){
    System.out.println("Enter number corresponding to your decade:");
    System.out.println("   1 - 1900-1909");
    System.out.println("   2 - 1910-1919");
    System.out.println("   3 - 1920-1929");
    System.out.println("   4 - 1930-1939");
    System.out.println("   5 - 1940-1949");
    System.out.println("   6 - 1950-1959");
    System.out.println("   7 - 1960-1969");
    System.out.println("   8 - 1970-1979");
    System.out.println("   9 - 1980-1989");
    System.out.println("   10 - 1990-1999");
    System.out.println("   11 - 2000-2005");
}

private static int decadeSelection(){
    String decadeChoice;
    int decade;

    do {
        System.out.println("Enter a decade: ");
        decadeChoice = keyboard.nextLine();

        decade = checkDecade(decadeChoice);

    } while (!validDecade);

    return decade;
}

private static int checkDecade(String decadeChoice){
    int decade = 0;

    try {
        decade = Integer.parseInt(decadeChoice);
    }
    catch (Exception e){
        System.out.println("That is not an integer. Please try again.");

        return decade;
    }

    if (decade < 1 || decade > 11){
        System.out.println("Enter an integer between 1 and 11");

        validDecade = false;
        return decade;
    }
    else {
        validDecade = true;

        return decade;
    }
}

validDecade is an instance boolean

Upvotes: 0

Views: 89

Answers (4)

chaos
chaos

Reputation: 681

If you're trying to create a command line menu, have you considered using the Scanner class?

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

It has a nextInt(), which will return the int for you and will figure out the int for you.

Upvotes: 0

koljaTM
koljaTM

Reputation: 10262

You can use the the Integer.parseInt() route and catch an exception if it isn't a number, but that is kinda messy. The other way would be to use a regular expression first to see if your String matches e.g. \d+ (or some more complex regex if you want to include negative and fractional numbers)

Upvotes: 0

user3015246
user3015246

Reputation: 139

You may try to check if the letters are all digits using the Character.isDigit() method

And yes, like the above user said, you can use a try-catch block for catching exceptions. Perhaps, assertions can also help.

Upvotes: 1

Robin Green
Robin Green

Reputation: 33033

Integer.parseInt will throw an exception if it's not an integer. You can catch that exception using a try ... catch block; if you catch the exception, that means it's not an integer. You could set the decade to an invalid value such as 0 in that case, to make things easier.

Upvotes: 1

Related Questions