Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
sir !
I have written the code in java for calculator using switch-case statement .
but i select any case it shows message "Oops! wrong choice" that i have wrote in default statement.
here's the program:

Java
import java.io.*;
class calculatorEx
{
public calculatorEx()
 {
      System.out.println("constructor invoked");
}
   
   public void calc()
   { 
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               int a, b,c, d,ch;
               try
               {
               
               System.out.println("Enter the value of a");
               a = Integer.parseInt(br.readLine());
               System.out.println("Enter the value of b");
               b = Integer.parseInt(br.readLine());
               System.out.println("Enter the value of c ");
               c = Integer.parseInt(br.readLine());
               System.out.println("\n-----Main Menu-----\n");
               System.out.println("1.Addition");
               System.out.println("2.Multiplication");
               System.out.println("3. Subtraction");
               System.out.println("4. Average");
               
               System.out.println("------------------------------------------------------------------------");
               System.out.println("\nEnter the function which u want to perform\n");
               ch = Integer.parseInt(br.readLine());
                 switch(ch)
               {
                   case '1':
                   d = a + b + c;
                   System.out.println("The result is " + d);
                   break;
                  case '2' :
                   d = a * b * c;
                   System.out.println("The result is " + d);
                   break;
                   case '3' :
                    d = a - b - c;
                    System.out.println(" The result is" + d);
                     break;
                     case '4':
                    d = a + b + c/2;
                    System.out.println("The result is " + d);
                    break;
                    default:
                    System.out.println(" Oops! wrong choice");
                        break;
                     } 
               }
              
               
              catch(IOException ca)
               {
                   System.out.println("Exception caught" + ca);
               }
}
              

        public static void main(String ar[])
        {
            calculatorEx calc1 = new calculatorEx();
             calc1.calc();
        }
}
Posted
Updated 24-Nov-11 9:11am
v2
Comments
tusharkaushik 24-Nov-11 15:17pm    
sir! how can i correct this code..
LanFanNinja 24-Nov-11 15:26pm    
Check solution below

As LanFanNinja suggested you shouldnot trying to compare an Integer to char literal. Try given code for switch block instead of your code.
C#
switch(ch)
               {
                   case 1:
                   d = a + b + c;
                   System.out.println("The result is " + d);
                   break;
                  case 2:
                   d = a * b * c;
                   System.out.println("The result is " + d);
                   break;
                   case 3:
                    d = a - b - c;
                    System.out.println(" The result is" + d);
                     break;
                     case 4:
                    d = a + b + c/2;
                    System.out.println("The result is " + d);
                    break;
                    default:
                    System.out.println(" Oops! wrong choice");
                        break;
                     }
               }


For More reference :-
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html[^]
 
Share this answer
 
Comments
LanFanNinja 24-Nov-11 16:23pm    
+5
RaviRanjanKr 25-Nov-11 0:21am    
Thanks :)
Nagy Vilmos 25-Nov-11 4:55am    
The average case is wrong, it should be:
d = (a + b + c) / 3;
RaviRanjanKr 25-Nov-11 7:31am    
Oops! My bad.. yeah you are right IO didn't notice over that. Thanks for mention here :)
Change your case statements from
Java
case '1':

to
Java
case 1:


you are trying to compare a integer to a char literal.
 
Share this answer
 
v3
Comments
RaviRanjanKr 24-Nov-11 16:01pm    
My 5+
LanFanNinja 24-Nov-11 16:02pm    
Thank you
LanFanNinja 24-Nov-11 16:01pm    
Why was this down voted! It IS the correct answer!!
TorstenH. 25-Nov-11 2:01am    
and the first correct one - my +5 for that! Should have been an accepted answer.
LanFanNinja 25-Nov-11 2:23am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900