Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
I'm having a trouble figuring out how to solve this issue when it just stops when I call this method when I input letters and other types. What is the problem with my code? Im using netbeans. Can anyone help? Thanks in advance!

Here is my code:

Java
public int selectOption(int maxRange, String sourceType) throws IOException
{
    do
    {
        try
        {
            userInput = input.nextInt();

        }//end try
        catch(Exception e)
        {
            validInput=false;
            input.next();

        }//end catch

        if (userInput<1 || userInput>maxRange)
        {
            MovieHouse.clearScreen();
            System.out.println("Select from the options only!");
            loadHeader();

            if(sourceType.equals("main"))
                MovieHouse.homeMenu();
            else if(sourceType.equals("movie menu"))
                loadMovieMenu();

        }//end if

    }while(userInput<1 || userInput>maxRange || validInput==false);

    return userInput;

}//end selectOption
Posted
Updated 6-Dec-13 2:30am
v3
Comments
Sergey Alexandrovich Kryukov 5-Dec-13 12:19pm    
What trouble, exactly? You need to start questions from the very beginning, not from the middle (the code): what did you try to achieve and how, what did your expect to see and what happened instead, why do you think it's wrong?
—SA
MLDS 8-Dec-13 3:53am    
Hi. My objective is to handle the exception and if the input of the user is not a number, it will run a loop to prompt the user to enter a correct number for the program to continue. I want to handle the exception while not ending the program. What would you suggest in this case?
Sergey Alexandrovich Kryukov 8-Dec-13 14:01pm    
You need 1) filter out unwanted characters from the very beginning; as the user types; how you do it depends on the UI framework you use; 2) when you are about to use the user data, validate it.
—SA

- why is there "throws" declaration when there's also a try/catch insde to prevent that?
- Why is the boolean "validInput" only touched in said catch? When will it be true?
- why are you reacting in if condition on "userInput<1 || userInput>maxRange" ? What happens to the rest?
- Why is the loop running as long as this condition is true (I'm assuming, cause "validInput" will never be true)?

I suggest to start over.

Also I declare this to be a basic homework task - please ask specific.
 
Share this answer
 
v2
Comments
MLDS 8-Dec-13 3:36am    
Hi. My objective is to handle the exception and if the input of the user is not a number, it will run a loop to prompt the user to enter a correct number for the program to continue. I want to handle the exception while not ending the program. What would you suggest in this case?
TorstenH. 11-Dec-13 6:53am    
Catching and Handling Exceptions

This is what you're asking for. But I don't know if that is already discuss in class.
Take a look at my recent article, hope it could give you some direction:
Code Experiment on Integer Validation
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900