Click here to Skip to main content
12,690,575 members (33,862 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
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:

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 24-Nov-11 9:42am
Updated 24-Nov-11 10: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

As LanFanNinja suggested you shouldnot trying to compare an Integer to char literal. Try given code for switch block instead of your code.
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[^]
  Permalink  
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 :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Change your case statements from
case '1':

to
case 1:


you are trying to compare a integer to a char literal.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170117.1 | Last Updated 24 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100