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;
...........
...........
}
}
Put it around the last getInput call, the one reading the user’s option.
LE:
This is what I originally meant.
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.