I posted a code on this link http://www.daniweb.com/forums/post915333.html#post915333 and the thread was solved, but I'm looking forward for another method using switch case.... but an error says :
incompatible types
found: java.lang.String
required: int
is the re a possible way that forces switch case method to use string instead of int???
this was the code that I want to experiment:
import java.io.*;
public class ifmdas3{
public static void main(String[] args) throws IOException{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String option, firstnum, secondnum;// trims leading & trailing spaces
int num1, num2, prod, quo, sum, diff;
System.out.print("Choose An Operation");
System.out.print("\nPress1 for Multiplication");
System.out.print("\nPress2 for Division");
System.out.print("\nPress3 for Addition");
System.out.print("\nPress4 for Subtraction");
System.out.print("\n\nOperation: ");
option=stdin.readLine();
String choice = option.trim();
/*if(choice.equalsIgnoreCase("m")){
}else if(choice.equalsIgnoreCase("d")){
}
else if(choice.equalsIgnoreCase("a")){
}
else if(choice.equalsIgnoreCase("s")){
}
else
{
}*/
switch("choice")
{
case 'm':
case 'M':
System.out.print("\nEnter the first number");
firstnum=stdin.readLine();
System.out.print("\nEnter the second number");
secondnum=stdin.readLine();
num1=Integer.parseInt(firstnum);
num2=Integer.parseInt(secondnum);
prod=num1*num2;
System.out.print("\nThe product is: "+prod);
break;
case 'd':
case 'D':
System.out.print("\nEnter the first number");
firstnum=stdin.readLine();
System.out.print("\nEnter the second number");
secondnum=stdin.readLine();
num1=Integer.parseInt(firstnum);
num2=Integer.parseInt(secondnum);
if(num2==0){
System.out.print("cannot perform the operation");
}else{
quo=num1/num2;
System.out.print("\nThe quotient is: "+quo);
}
break;
case 'a':
case 'A':
System.out.print("\nEnter the first number");
firstnum=stdin.readLine();
System.out.print("\nEnter the second number");
secondnum=stdin.readLine();
num1=Integer.parseInt(firstnum);
num2=Integer.parseInt(secondnum);
sum=num1+num2;
System.out.print("\nThe sum is: "+sum);
break;
case 's':
case 'S':
System.out.print("\nEnter the first number");
firstnum=stdin.readLine();
System.out.print("\nEnter the second number");
secondnum=stdin.readLine();
num1=Integer.parseInt(firstnum);
num2=Integer.parseInt(secondnum);
diff=num1-num2;
System.out.print("\nThe diference is: "+diff);
break;
default:
System.out.print("Invalid input for the choice of operation");
break;
}
}
}