-3

My programme has an option of between 1-5

If someone enters a letter like S the code will break how can i stop this. can someone give me the code

I added a

if (ticket > 5) {
                JOptionPane.showMessageDialog(null, "Invalid ticket type selected (1-5)");

so if the user inputs 6 it will give him an error as ticket is bigger then 5 but how can i do this with a alphabetical letter thanks

dragon
  • 1
  • 1
  • 1
    You need to check the value before parsing it as `int`. Check if it can be parsed before actually parsing it. How do you parse it? Can you please also show that code, it actually is the relevant part for your question. If you use `Integer.parseInt(value)` then you might want to use `try-catch` and `NumberFormatException`. If you reach the `catch`-block the number was **invalid** and could not be parsed as `int`. – Zabuzard Oct 18 '17 at 18:47

3 Answers3

1

You need to check the value before parsing it as int. Check if it can be parsed before actually parsing it.

If you use Integer.parseInt(value) then you might want to use try-catch and catch NumberFormatException. If you reach the catch-block the number was invalid and could not be parsed as int. To quote from its documentation:

NumberFormatException - if the string does not contain a parsable integer.

Here is how you could do it:

String valueFromUser = ...

try {
    int ticket = Integer.parseInt(valueFromUser);

    if (ticket < 1 || ticket > 5) {
        JOptionPane.showMessageDialog(null,
            "Invalid ticket type selected (1-5)");
    }

    // The rest of your code
} catch (NumberFormatException e) {
    // Ticket could not be parsed
    JOptionPane.showMessageDialog(null,
        "Invalid ticket, please input a number (1-5)");
}

For more checks you might want to take a look at: Determine if a String is an Integer in Java

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

just use this if statement:

if( ticket > 5 || ticket < 1){
   JOptionPane.showMessageDialog(null, "Invalid ticket type selected (1-5)");
}

Then, you can add an else statement for what ever you want to do with valid ticket values.

if( ticket > 5 || ticket < 1){
   JOptionPane.showMessageDialog(null, "Invalid ticket type selected (1-5)");
} else {
   //You logic goes here
}

You have the basic structure in your code, but you just have to think about all the cases rather than a ticket just being greater than 5.

Edit: As the other answers have pointed out, you should also be checking if ticket is of type int. That way, you can provide more specific error messages to the user.

Cody Ferguson
  • 393
  • 1
  • 4
  • 20
  • 1
    Note that OP allowed values `5` and `1`. You should replace `>=` and `<=` with `>` and `<`. – Zabuzard Oct 18 '17 at 18:53
  • Apart from the question being unclear, did you read it? This was not the question. – BackSlash Oct 18 '17 at 18:53
  • Yes, I read the question. He was asking how to check if ticket had a valid value. Now, I did not include the check to see if ticket was of type int, but I did cover what the if statement should be to only allow ticket values of 1 - 5, inclusively. I will add a comment that the OP should follow the other answers for type checking solution. – Cody Ferguson Oct 18 '17 at 19:27
0

See if this help you:

    char ticket = (char) System.in.read();

    boolean isDigit = Character.isDigit(ticket);

    if (isDigit) {
        int option = Integer.parseInt(String.valueOf(ticket));

        if (option > 5 | option < 1) {
            JOptionPane.showMessageDialog(null, "Invalid ticket type selected (1-5)");
        } else {
            JOptionPane.showMessageDialog(null, "Congrats you choosen " + option);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Invalid ticket type selected (1-5)");
    }
Duloren
  • 2,395
  • 1
  • 25
  • 36