Results 1 to 18 of 18
  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question Calculator using Switch

    Hey guyz,
    so basically user enters 3 values , 2 integer and one character that'll be used for switch case .
    lets say i enter 2 and 3 and + , the output will be 2+3=5

    take a look at my code :


    package quiz1;
    import java.util.Scanner;

    Java Code:
    public class calculator {
    public static void main (String [] args){
        Scanner sc=new Scanner(System.in);
        int a,b;
        char p='+';
        char m='-';
        char multi='*';
        char div='/';
        String character;    
        
            System.out.print("enter ur firt value : ");
            a=sc.nextInt();
            System.out.print("enter ur second value : ");
            b=sc.nextInt();
            System.out.print("enter ur operator +,-,*,/ : ");
            character=sc.nextLine();
        
        
        
            switch(p){
                case '+':
                    System.out.print(a+b);
                    break;
                
                            
            }
        
           
        }    
    }

    i need to know how can i get character input from user that later would be used in switch case.
    P.S: im using Scanner , i dont wanna user buffer reader.
    thanks in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Calculator using Switch

    'character' is a String.
    The String API has lots of methods on it, some of which return chars.
    And before you ask "why don't you just tell me what I need to do", learning how to read the API and search it for useful methods is a key skill in any software development.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    i used string
    Java Code:
     x=sc.next();
    to get a character input from user.
    it works fine for sum operator , tho for substraction it does the same function as sum , is the problem with my switch ?



    Java Code:
    package quiz1;
    import java.util.Scanner;
    
    public class calculator {
    public static void main (String [] args){
        Scanner sc=new Scanner(System.in);
        int a,b;
        char p='+';
        char m='-';
        char multi='*';
        char div='/';
        String character;    
        
            System.out.print("enter ur firt value : ");
            a=sc.nextInt();
            System.out.print("enter ur second value : ");
            b=sc.nextInt();
            System.out.print("enter ur operator +,-,*,/ : ");
            character=sc.next();
        
        
        
            switch(p){
                case '+':
                    System.out.print(a+b);
                    break;
                
                        
            }
        switch(m){
            case 'm':
                System.out.print(a-b);
                break;
        }
           
        }    
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calculator using Switch

    Print out the values for variables 'm' and 'p' and see why your code does what it does.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Calculator using Switch

    I think you need to read up on how switch works.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    Quote Originally Posted by JosAH View Post
    Print out the values for variables 'm' and 'p' and see why your code does what it does.

    kind regards,

    Jos
    i dont know if i did the right thing ,

    Java Code:
      switch(p){
                case '+':
                    System.out.print(p);
                    break;
                
                        
            }
        switch(m){
            case 'm':
                System.out.print(m);
                break;
        }
    the out put is only + sign.

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    i modified my switch into this :

    Java Code:
    switch(chacacter){
                case '-':
                    System.out.print(a-b);
                    break;
                case '+':
                    System.out.print(a+b);
                    break;
                case '*':
                    System.out.print(a*b);
                    break;
                case '/':
                    System.out.print(a/b);
                    break;
                default :
                    System.out.print("syntax error");
                    break;
                        
            }
    the problem is that i dont know how to put all variables into one switch , e.g : switch(p,m,multi,div)
    can u guide through this?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calculator using Switch

    Quote Originally Posted by pinkdreammsss View Post
    i dont know if i did the right thing ,

    Java Code:
      switch(p){
                case '+':
                    System.out.print(p);
                    break;
                
                        
            }
        switch(m){
            case 'm':
                System.out.print(m);
                break;
        }
    the out put is only + sign.
    So the value of variable 'm' definitely isn't 'm' (otherwise it would've been printed). Move those print statement before your switch statement and see ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    Quote Originally Posted by pinkdreammsss View Post
    i modified my switch into this :

    Java Code:
    switch(chacacter){
                case '-':
                    System.out.print(a-b);
                    break;
                case '+':
                    System.out.print(a+b);
                    break;
                case '*':
                    System.out.print(a*b);
                    break;
                case '/':
                    System.out.print(a/b);
                    break;
                default :
                    System.out.print("syntax error");
                    break;
                        
            }
    the problem is that i dont know how to put all variables into one switch , e.g : switch(p,m,multi,div)
    can u guide through this?
    Any idea about this ?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Calculator using Switch

    You don't put all those variables in.
    You want to switch on the character the user enters (ie the first char of the 'character' string).
    Each 'case' is the thing you want to check.
    So your switch is almost correct, you just need to have it switching on a single character, for which you need to read the API I linked to above.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    you just need to have it switching on a single character
    does that mean that i have to use only one character, containing all +-*/ ? probably not , cause char can only contain one value.

    i know the problem is with this :

    Java Code:
    switch(m)
    i need to know how to implement all of the following :

    Java Code:
     char p='+';
        char m='-';
        char multi='*';
        char div='/';
    into one switch , so when user press + or - etc...
    one these cases function :

    Java Code:
       case '-':
                    System.out.print(a-b);
                    break;
                case '+':
                    System.out.print(a+b);
                    break;
                case '*':
                    System.out.print(a*b);
                    break;
                case '/':
                    System.out.print(a/b);
                    break;
    PS: didnt find what i was looking for in the API uve linked above , couldnt find anythin related to characters or strings or user input.

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Calculator using Switch

    Well I do not see the problem, maybe you should post what happens and what is the problem now - it seems the original problem was solved.
    Last edited by Sierra; 04-12-2012 at 07:30 PM.

  13. #13
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    Quote Originally Posted by Sierra View Post
    Well I do not see the problem, maybe you should post what happens and what is the problem now - it seems the original problem was solved.
    there's a problem with my switch , ive alredy explained why above , any idea?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Calculator using Switch

    The value in the 'switch' is the variable you are checking.
    SO you want to check the user input, not some value you have stored.
    The values in the 'case' are the ones you want to compare against.
    It's essentially a great big if/elseif/else statement.
    Java Code:
    switch(thingIWantToCheck) {
       case 'a': System.out.println("It's an a!");
          break;
       case 'b': System.out.prinltn("It's a b!");
          break;
       default: System.out.println("boo!");
    }
    Is the same as doing:
    Java Code:
    if (thingIWantToCheck == 'a') {
       System.out.println("It's an a!");
    } else if (thingIWantToCheck  == 'b') {
       System.out.println("It's an b!");
    } else {
       System.out.println("boo!");
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    thanks for the hints guyz. the problem with my program was that i stored values as char , but the input from user was String , so i just used String for all + - * /.
    here's my program :

    Java Code:
    package quiz1;
    import java.util.Scanner;
    
    public class calculator {
    public static void main (String [] args){
        Scanner sc=new Scanner(System.in);
        int a,b;
        String p="+";
        String m="-";
        String multi="*";
        String div="/";
        String character;    
        
            System.out.print("enter ur firt value : ");
            a=sc.nextInt();
            System.out.print("enter ur second value : ");
            b=sc.nextInt();
            System.out.print("enter ur operator +,-,*,/ : ");
            character=sc.next();
        
        
            switch(character){
                case "-":
                    System.out.print(a-b);
                    break;
                case "+":
                    System.out.print(a+b);
                    break;
                case "*":
                    System.out.print(a*b);
                    break;
                case "/":
                    System.out.print(a/b);
                    break;
                default :
                    System.out.print("syntax error");
                    break;
                        
            }
       
           
        }    
    }

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Calculator using Switch

    I hope you're aware that switching on String was introduced in Java 7.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Calculator using Switch

    Quote Originally Posted by DarrylBurke View Post
    I hope you're aware that switching on String was introduced in Java 7.

    db
    i didnt know that.

  18. #18
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Calculator using Switch

    Guy, you have posted so many versions of your code that it is difficult to determine which one has which problem and what actual problem you are referring to... IF you have a problem you post the code and then you answer questions to clarify what the problem is... it does barely work any other way round. Usually a "problem" has an error message or a wrong output. I do not see any of those posted here.

Similar Threads

  1. Switch
    By java4amanda in forum New To Java
    Replies: 13
    Last Post: 03-21-2012, 09:53 AM
  2. Switch between forms.
    By OMFGITSROHIT in forum AWT / Swing
    Replies: 9
    Last Post: 03-13-2010, 07:47 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •