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 8892279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:57:36+00:00 2026-06-14T22:57:36+00:00

I am actually having trouble with my program. Actually I study in school (11

  • 0

I am actually having trouble with my program. Actually I study in school (11 grade- junior college) so please explain me in very simple language. I am developing a quiz, very basic school project and so the programs goes on like this…. I want the user to enter his/her choice, by inputting numbers from 1-4. I don’t want the user to enter an alphabet, letter or special character or any other no. other than 1-4. I tried using try and catch but my program stops after throwing the exception. I want the program code to run even after displaying the error message System.out.println(” Invalid input. Please enter a number between 1-4″);
sample program

import java.io.*;
  import java.lang;
  public class
  { 
   public static void main()throws IOException
   { 
     InputStreamReader read =new InputStreamReader (System.in);
     BufferedReader in =new BufferedReader (read);
     System.out.println(" Select your category");
     System.out.println(" 1. Food");
     System.out.println(" 2. National");
     System.out.println("");
     System.out.println(" Enter your choice"); 
     int choice=Integer.parseInt(in.readLine());
     if(choice==1)//food category
     { 
       int score = 0;
       System.out.println("what are dynamites made?");
       System.out.println("1. peanuts");System.out.println("2. grapes");
       System.out.println("3. flaxseeds");System.out.println("4. fish");
       System.out.println(" Enter your choice");
       int food1= Integer.parseInt(in.readline()); 
      if(c1=1)
      { System.out.println(" Correct answer");
        score=score+10
      }
       else
      { 
      System.out.println(" Wronge answer");
        score=score+0
      }
      //then i have the second question with the same format
     } 
      if(choice==2)//natioanl category with the same format as above
      {//two question with if else statements in them }
     }
}// also help me where to add the try and catch statements
  • 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-14T22:57:38+00:00Added an answer on June 14, 2026 at 10:57 pm

    Here’s a more elegant solution to this problem. No try-catch involved.

    import java.io.*;
      import java.lang;
      public class
      { 
         //Compares response to the string representations of all numbers between 1 and numOptions, inclusive. 
         //Returns the matching integer or -1 otherwise.
         public static int parseResponse(String response, int numOptions) 
         {
              for(int i=1;i<=numOptions;i++) {
                  if(response.equals(Integer.toString(i))) return i;
              }
              return -1;
         }
    
         public static void main()throws IOException
         { 
             InputStreamReader read =new InputStreamReader (System.in);
             BufferedReader in =new BufferedReader (read);
             System.out.println(" Select your category");
             System.out.println(" 1. Food");
             System.out.println(" 2. National");
             System.out.println("");
             System.out.println(" Enter your choice");
    
             //New code
             int choice=-1;
             while(choice==-1) {
                 choice=parseResponse(in.readLine(),2);
                 if(choice==-1)
                 {
                     System.out.println(" Invalid input. Please enter a number between 1-2");
                 }
             }
    
    
             if(choice==1)//food category
             { 
                 int score = 0;
                 System.out.println("what are dynamites made?");
                 System.out.println("1. peanuts");System.out.println("2. grapes");
                 System.out.println("3. flaxseeds");System.out.println("4. fish");
                 System.out.println(" Enter your choice");
    
                 //New code
                 int food1=-1;
                 while(food1==-1) {
                     food1=parseResponse(in.readLine(),4);
                     if(food1==-1)
                     {
                         System.out.println(" Invalid input. Please enter a number between 1-4");
                     }
                 }
    
                 if(food1==1)
                 { System.out.println(" Correct answer");
                   score=score+10
                 }
                 else
                 { 
                   System.out.println(" Wronge answer");
                   score=score+0
                 }
                 //then i have the second question with the same format
            } 
            if(choice==2)//natioanl category with the same format as above
            {//two question with if else statements in them 
            }
        }
    }// also help me where to add the try and catch statements
    

    In case you don’t know:

     if(response.equals(Integer.toString(i))) return i;
    

    is the same as

     if(response.equals(Integer.toString(i)))
     {
          return i;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program generates relatively simple PDF documents on request, but I'm having trouble with
This seems simple to me but I'm having trouble actually finding anything explicitly stating
I'm having trouble with a simple threading example. I know my methods aren't complete,
It might seem a little dull and basic, but I'm actually having trouble to
I am having trouble turning strings to uppercase for an alphabetical sort. My program
I'm having trouble figuring out what a commandResource actually looks like. The only example
I'm having trouble with a little program I'm making. Basically I have 6 Classes.
I am working with a Swing program and having a little trouble. The program
I am actually having a list of text boxes. I am using below code
I'm having a rare CSS issue in Internet Explorer 7 (I'm actually testing it

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.