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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:12:22+00:00 2026-06-17T12:12:22+00:00

I am trying to create a loop with the switch statement. If user does

  • 0

I am trying to create a loop with the switch statement. If user does not enter between 1 and 4 a message of “You enter incorrect option” keeps re-occurring. I am working on Lynda video. I am not sure where to put the loop. I cannot currently find a way to get it to loop. Is it in the switch or in the getInput method. Is it possible to do this? Thanks in advance if anyone knows. I am using eclipse, java 7.

  public static void main(String[] args) {
    String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");



  double result = 0;
   do  { 
           String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
           int opInt = Integer.parseInt(op);
           switch (opInt) 
           {
             case 1:
             result = addValues(s1, s2);
             break;
           }
        } while(opInt<1 || opInt >4);

EDIT
error message of…

Multiple markers at this line
– opInt cannot be resolved to a
variable

  • opInt cannot be resolved to a
    variable

    //I have a method called addValues among other mathematic operators

    private static double addValues(String s1, String s2)
    throws NumberFormatException {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 + d2;
    return result;
    }
    
    private static String getInput(String prompt) {
    BufferedReader stdin = new BufferedReader(
            new InputStreamReader(System.in));
    
    System.out.print(prompt);
    System.out.flush();
    
    try {
        return stdin.readLine();
    } catch (Exception e) {
        return "Error: " + e.getMessage();
    }
    }
    

    EDIT
    I am having a problem with the solution given to me by Tal and Quoi. With Quoi I am getting error of opInt cannot be resolved to a variable because and with Tal nothing happens.

So I have done the following…

String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");
    String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");

//convert opInt into integer
    int opInt = Integer.parseInt(op);

    if (opInt <1 || opInt >4) // used a if statement
     {
      getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
     }

    double result = 0;
    {
         switch (opInt)
         {
           ...........
           ...........
         }
    }

With the if statement, the method calls back the switch statement but after selecting the correct option the printline of “You enter incorrect option” and “the answer is 0.0” displays so therefore no calculation is done. I am not sure if this is a simple solution or what?

EDIT
I have now attempted to do a while loop outside the switch. What is happening is that what when I select outside of 1 or 4 I am getting a re-ourrance of message of “Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide” regardless if I enter valid option.

do 
{
  getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
 } while (opInt <1 || opInt >4);

    double result = 0;
    {
       switch (opInt)
       {
           case 1:
            result = addValues(s1, s2);
            break;
             ...........
             ...........
         }
    }
  • 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-17T12:12:23+00:00Added an answer on June 17, 2026 at 12:12 pm

    Put it around the last getInput call, the one reading the user’s option.

    LE:

    This is what I originally meant.

     String s1 = getInput("Enter a numeric value: ");
     String s2 = getInput("Enter a numeric value: ");
    
     int opInt = 0;
     do {
       String op = getInput("Enter 1=Add, 2=Subtract, 3=Multiply, 4=Divide");
       try {
         opInt = Integer.parseInt(op); 
       }catch(Exception e) {
         //whatever you need to do
       }
     } while (opInt < 1 || opInt > 4);
    
     switch(opInt) [...]
    

    What is happening here: user enters the 2 numbers then it will be asked to enter his option until it is between 1 and 4.

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

Sidebar

Related Questions

I am trying to create a loop statement for some of my code and
I'm trying to create a loop that creates a series of objects that contains
I'm trying to create a loop than continues to take input until the input
I am trying to create a loop function based on the time. After the
New to javascript and such, trying to create a loop for fading logos using
I'm trying to create a for loop with NSDates, so that the loop would
I am running a loop, and I am trying to create a variable each
I'm trying to loop through a number of items, and create a json object.
Im trying to create a loop of items like this... for each (var btn:Object
I'm trying to create a loop that will output multiple where statements for a

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.