Results 1 to 18 of 18
Thread: Calculator using Switch
- 04-12-2012, 10:53 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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.
- 04-12-2012, 11:20 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-12-2012, 12:13 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Re: Calculator using Switch
i used string
to get a character input from user.Java Code:x=sc.next();
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; } } }
- 04-12-2012, 12:30 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Calculator using Switch
Print out the values for variables 'm' and 'p' and see why your code does what it does.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-12-2012, 12:38 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-12-2012, 12:43 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
- 04-12-2012, 01:15 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Re: Calculator using Switch
i modified my switch into this :
the problem is that i dont know how to put all variables into one switch , e.g : switch(p,m,multi,div)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; }
can u guide through this?
- 04-12-2012, 01:20 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Calculator using Switch
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-12-2012, 01:54 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
- 04-12-2012, 02:46 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-12-2012, 03:16 PM #11
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Re: Calculator using Switch
does that mean that i have to use only one character, containing all +-*/ ? probably not , cause char can only contain one value.you just need to have it switching on a single character
i know the problem is with this :
i need to know how to implement all of the following :Java Code:switch(m)
into one switch , so when user press + or - etc...Java Code:char p='+'; char m='-'; char multi='*'; char div='/';
one these cases function :
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.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;
- 04-12-2012, 07:27 PM #12
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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.
- 04-13-2012, 07:58 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
- 04-13-2012, 10:15 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
Is the same as doing: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!"); }
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.
- 04-13-2012, 12:08 PM #15
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
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; } } }
- 04-13-2012, 12:15 PM #16
Re: Calculator using Switch
I hope you're aware that switching on String was introduced in Java 7.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-13-2012, 12:26 PM #17
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
- 04-13-2012, 02:19 PM #18
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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
-
Switch
By java4amanda in forum New To JavaReplies: 13Last Post: 03-21-2012, 09:53 AM -
Switch between forms.
By OMFGITSROHIT in forum AWT / SwingReplies: 9Last Post: 03-13-2010, 07:47 PM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks