Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:13:10+00:00 2026-06-16T09:13:10+00:00

I need help Using Exception handling with Wrong User Input. I am creating a

  • 0

I need help Using Exception handling with Wrong User Input. I am creating a text based game that welcomes the User and then goes to the main menu. It then tell the User the options, and then look for User input. For some Reason, whenever I input 1 or 2, It says: “Your input is invalid, please try again” And goes back to the choices. I don’t know exactly where I am going wrong, hopefully someone can help me. Also, it won’t catch the Mismatch Exception either. Hope you can Help! Thanks, Shandan

   public static void main(String[] args) {



    System.out.println("Welcome to Spec Ops!");
    System.out.println("Please state your name:");
    Scanner name = new Scanner(System.in);
    String Name = name.next();

    System.out.println("Hello "+Name);
    mainMenu();

    }


public static void mainMenu() {

    System.out.println("1. Story Mode");
    System.out.println("2. Infinant Combat");



    Scanner input = new Scanner(System.in);



    Object Selection = input.nextInt();

    boolean validOption = true;
    Integer x;


    try {
    x = (Integer)Selection;
    } catch(ClassCastException cce){
        System.out.println("Your input is invalid, please try again");
        validOption = false;
    } catch(InputMismatchException ime){
        System.out.println("Your input is invalid, please try again");
        validOption = false;

    }


if(validOption) {
    System.out.println("Hello!");
}

else {
    mainMenu();
}

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:13:11+00:00Added an answer on June 16, 2026 at 9:13 am

    Scanner.nextInt returns ant int, so there is no need to go Object selection = scanner.nextInt(), and then cast to an int, you can merely have int selection = scanner.nextInt() and surround that in a try catch that chatches java.util.InputMismatchException, which is the exception thrown when the user types a letter and not an number

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with using VQmod, if anyone has any experience with, I'd appreciate
I need some help using regex in java. Im trying to achieve 2 different
Possible Duplicate: Regular expression: match all words except I need your help for using
Need help writing a script downloads data from google insight using c# this is
i need help with this. I'am using JQuery's Dynamic Form plug-in, it duplicates my
We are using Linq-to-SQL with stored Procedures we need help on one of our
I need help reading the contents of a webpage. Currently i am using the
I need help in jquery. Thanks in advance. I am using autocomplete function of
I really need help this time - Joomla 2.5 and latest K2. I'm using
I need some help with overlaying views using the prism framework.Its a little more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.