3 Replies - 114 Views - Last Post: 27 February 2012 - 10:45 AM Rate Topic: -----

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 21-November 11

Switch default case catches numbers but not characters

Posted 27 February 2012 - 10:13 AM

A user is presented with the following message.

Select an option; (1) Program, (2) Picture or (3) Book

This is returned to the user interface as a string.

 private String process_And_Display_Options(String theInput) {
        String theOutput;
        int fileArrayIndex;
        FileConfiguration fc = new FileConfiguration();

        // Create Int from selected option 
        int selection = Integer.parseInt(theInput);

        switch (selection) {
            case 1:
                fileArrayIndex = 0;

                get_And_Display_Download_Info();
                theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                state = State.Another;
                break;
            case 2:
                fileArrayIndex = 1;

                get_And_Display_Download_Info();
                theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                state = State.Another;
                break;
            case 3:
                fileArrayIndex = 2;

                get_And_Display_Download_Info();
                theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                state = State.Another;
                break;
            default:
                theOutput = INVALID_CHOICE + fc.fileOptionsArrayToString();
                state = State.Selecting;
                break; 
        }
        return theOutput;
    }


If a user types in a number that doesn't match 1, 2 or 3 the default case returns an error message and prompts the user to try again.

However, if a user types in a character, the application causes an exception and crashes. I'd imagine this is because I am trying to turn a character into a String which isn't going to go down well. Any ideas how I can ensure that the default case is reached if the user enters a character?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Switch default case catches numbers but not characters

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Switch default case catches numbers but not characters

Posted 27 February 2012 - 10:20 AM

Put the parse and the switch in a try catch statement - in the catch portion have it display the message. It will not continue in the try if it runs into an error.
So -
try{
       int selection = Integer.parseInt(theInput);
       switch (selection) {
 /* rest of code here */
}catch(InputMismatchException e)
{
    // the message for incorrect format.
}

Was This Post Helpful? 1
  • +
  • -

#3 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 21-November 11

Re: Switch default case catches numbers but not characters

Posted 27 February 2012 - 10:39 AM

View PostFuzzyness, on 27 February 2012 - 10:20 AM, said:

Put the parse and the switch in a try catch statement - in the catch portion have it display the message. It will not continue in the try if it runs into an error.
So -
try{
       int selection = Integer.parseInt(theInput);
       switch (selection) {
 /* rest of code here */
}catch(InputMismatchException e)
{
    // the message for incorrect format.
}


How does this work with the return statement. Whenever I add the catch I either get method missing return statement or output the other alternative is output has not been declared.

   private String process_And_Display_Options(String theInput) {

        try {

            String theOutput;
            int fileArrayIndex;
            FileConfiguration fc = new FileConfiguration();

            // Create Int from selected option 
            int selection = Integer.parseInt(theInput);

            switch (selection) {
                case 1:
                    fileArrayIndex = 0;

                    get_And_Display_Download_Info();
                    theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                    state = State.Another;
                    break;
                case 2:
                    fileArrayIndex = 1;

                    get_And_Display_Download_Info();
                    theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                    state = State.Another;
                    break;
                case 3:
                    fileArrayIndex = 2;

                    get_And_Display_Download_Info();
                    theOutput = fc.fileDescription[fileArrayIndex] + CONTINUE_OPTIONS;
                    state = State.Another;
                    break;
                default:
                    theOutput = INVALID_CHOICE + fc.fileOptionsArrayToString();
                    state = State.Selecting;
                    break;
            }

            return theOutput;
            
        } catch (Exception e) {
            // invalid message here
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Switch default case catches numbers but not characters

Posted 27 February 2012 - 10:45 AM

Just move the line to return to outside of the catch. It will still execute.

When it goes through the method, it will start the try, if that throws an error it will run the catch, then when thats done it will run the rest of the code inside of the method. So if you move the return statement to outside of the try then it will work.

     } catch (Exception e) {
         // invalid message here
     }
    // return statement should go here
 }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1