if...else
Hi,
I just completed an assignment for school to write a program
that resembles a calculator.
This is a beginners class and we are about 3 weeks in to it.
The constraints are: to use onlyswitch,compareTo andif...else. It compiles and runs just fine.
But I'd like to add an error message. Just for the fun of it.
Something along the lines of
[pseudo-code]if(number1 or number2 is not equal any number)
print(error message);[/pseudo-code]
Is there any way to specify a set of all numbers in an if...expression?
I have heard about regEx but can't quite figure out how to apply it.
Also, I'd very much appreciate any sort of comments or criticism on the way I did this project or the readability. Thanks.
package calculator;
import java.util.*;
import java.text.DecimalFormat;
publicclass Calculator{
static Scanner sc =new Scanner(System.in);
static DecimalFormat dF =new DecimalFormat("0.000");
publicstaticvoid main(String[] args){
String oper;
double num1;
double num2;
System.out.print("Pretend I'm a real calculator.\n" +
"Enter your first number: ");
num1 = sc.nextDouble();
System.out.print("Enter the second number: ");
num2 = sc.nextDouble();
System.out.print("Enter operator: + (add), - (subtract)," +
"* (multiply), / (divide): ");
oper = sc.next();
if(num1| num2 != ?)
System.out.print("no");
//outputs error message
if ((oper.compareTo("/")==0&&num2==0))
System.out.print("\n\nSorry... in this universe; you" +
" can't divide buy ZERO!\nPlease exit and try again.\n");
//echos usr input
else System.out.println("You entered: " + num1+" " + oper+" " + num2);
switch(oper.compareTo("*"))
{
case 0: System.out.println(num1+" * "+num2+" = "+dF.format(num1 * num2));
break;
case 1: System.out.println(num1+" + "+num2+" = "+dF.format(num1 + num2));
break;
case 3: System.out.println(num1+" - "+num2+" = "+dF.format(num1 - num2));
break;
case 5: System.out.println(num1+" / "+num2+" = "+dF.format(num1 / num2));
break;
default: System.out.println("\n\n[]IF @OU *$##((('R'))) READING'_+*&^ "
+"t^HIS ME$$AGE... \n SOM3ThInG HA5 gGgONE"
+" S_E_R_I_O_U_S_L_Y wrong.\n S_L_O_W_L_Y stTTep"
+" away from the c%mp%ter...\n "
+"ndA FInD SHELtR IMMEDIATLY!!!");
}
}
}
Message was edited by:
ThatJive
[4417 byte] By [
ThatJivea] at [2007-10-3 5:22:17]

Look at the API docs for java.util.Scanner and you will see that there are a couple of ways of telling if the input is not a number: The hasNextDouble method, or the exception thrown by nextDouble.
You should probably also check that the operator is valid at some point before trying to use it
You want criticism? Sure. Get rid of that fucking annoying default message.
Hey, if you cant have fun with this stuff you might as well go back to sleep
Thanks for your replies everyone
> You should probably also check that the operator is
> valid at some point before trying to use it
the if...else expression checks the operator validity. if anything is entered other than +, -, *, or /
it returns the error message about not dividing by zero.
I'll check out hasNextDouble() when I get to lab today.
Thanks for your time.
I'll try not to be so irritating next post
seemed funny to me when I wrote it.
I guess I'm easily amused.
> the if...else expression checks the operator
> validity. if anything is entered other than +, -, *,
> or /
> it returns the error message about not dividing by
> zero.
No it doesn't. It checks to see if the operator is / with a divisor of 0, but it lets everything else through. If, for instance, you enter "@" for an operator, it will go straight through the if/else and end up outputting your funny error message from the switch.
ooops..... you're right. Thanks
> You want criticism? Sure. Get rid of that> fucking annoying default message.There is never any reason to swear.
Yes there is. Reading his message made my eyes bleed and I felt the need to swear.
Let me clarify: I don't want to stifle your creativity but your style in the message was highly irritating. Why not use plain old text?
System.out.println("\n\nIf you are reading this message something has gone etc");
> > You want criticism? Sure. Get rid of that
> > fucking annoying default message.
>
>
> There is never any reason to swear.
Sure there is. It makes me feel better and helps me express myself. They're just words. It seems rather stupid to limit how we express ourselves because some people consider some words "bad."
jverda at 2007-7-14 23:29:19 >

Of course I could just be a bad racist man.
> There is never any reason to swear.u bad man racist ******
> > There is never any reason to swear.> > u bad man racist ******?!
> > > There is never any reason to swear.> > > > u bad man racist ******> > ?!Rajesh is a humorous (to some) persona that pops up here occasionally and calls people "bad man racist" for no apparent reason. It's his shtick.
jverda at 2007-7-14 23:29:19 >

> Sure there is. It makes me feel better and helps me
> express myself. They're just words. It seems rather
> stupid to limit how we express ourselves because some
> people consider some words "bad."
Especially when these same people have no problem allowing you to express the exact same thought with different words. I find it ironic to be told that those who curse are only displaying a lack of vocabulary. Really? I fail to see how arbitrarily limiting your vocabulary displays a profound knowledge of language.
That being said the underlying problem in my eyes is people spewing vulgar thoughts in any form. It just so happens society lumps them into a set of bad words rather than addressing the content.
> That being said the underlying problem in my eyes is
> people spewing vulgar thoughts in any form. It just
> so happens society lumps them into a set of bad words
> rather than addressing the content.
You only think that because you're a ****-*******, *******-headed ****** ********er and you probably ******* with your ********* ******* while ********** ********* up your *********.
jverda at 2007-7-21 11:01:01 >

Careful jeff you might wear out your shift and 8 keys.
> Careful jeff you might wear out your shift and 8 keys.So that''s what the kids are calling it these days.
Don't use:
((oper.compareTo("/")==0)
Use:
(oper.equals("/"))
It is much clearer as to the intent. This is *not* C or C++. This is Java. "equals" exists for Strings, and you should use it.
Also, the Scanner will choke (throw an exception) if the input for num1 or num2 is not a number. So, rather than testing for what the input is yourself, you should catch that exception (I don't know offhand what exception is thrown, but the Scanner API will tell you).
MLRona at 2007-7-21 11:01:01 >

> Don't use:
> ((oper.compareTo("/")==0)
> Use:
> (oper.equals("/"))
>
> It is much clearer as to the intent. This is *not* C
> or C++. This is Java. "equals" exists for Strings,
> and you should use it.
>
This is true but...
"The constraints are: to use only switch, compareTo and if...else. "
> This is true but...
>
> "The constraints are: to use only switch, compareTo and if...else. "
Oops! Also, I just noticed the nasty input for the switch statement:
switch(oper.compareTo("*")) // Blech!!!!!
{
case 0: System.out.println(num1+" * "+num2+" = "+dF.format(num1 * num2));
break;
case 1: System.out.println(num1+" + "+num2+" = "+dF.format(num1 + num2));
break;
case 3: System.out.println(num1+" - "+num2+" = "+dF.format(num1 - num2));
break;
case 5: System.out.println(num1+" / "+num2+" = "+dF.format(num1 / num2));
How about using a switch on the operator itself? I wonder if calling "charAt" on a String is allowed.
MLRona at 2007-7-21 11:01:01 >

Thanks for all the replies. This is exactly the kind of help I was hoping for.
I imagine the constraints are there to help us really understand the concepts
at a deep level. But after 3 weeks or so, It is dawning on me; that just because it works
doesn't mean it is good, efficient or pretty.
I made a copy of my program to try out your suggestions. I knew I had to have an integer to use
switch but I couldn't figure out how to turn my (oper) string into one, other than the way I did it.
I guess I had charAt, indexOf and valueOf mixed up in my mind. Not any more.
I wonder why there isn't
static Scanner sc = new Scanner(System.in);oper = sc.nextChar
Anyway, I dug into the docs for charAt, thanks for the suggestion, is this what you had in mind?
switch(oper.charAt(0))
{
case 42: System.out.println(num1+" * "+num2+" = "+dF.format(num1 * num2));
break;
case 43: System.out.println(num1+" + "+num2+" = "+dF.format(num1 + num2));
break;
case 45: System.out.println(num1+" - "+num2+" = "+dF.format(num1 - num2));
break;
case 47: System.out.println(num1+" / "+num2+" = "+dF.format(num1 / num2));
break;
default: System.out.println("\n\n......
Even though I may not be able to use this in my assignment;
everyones comments introduced me to ideas I havent come across yet and
will help me write with (hopefully) more clarity down the road.
thanks again
> Thanks for all the replies. This is exactly the kind
> of help I was hoping for.
>
> I imagine the constraints are there to help us
> really understand the concepts
>
> at a deep level. But after 3 weeks or so, It is
> dawning on me; that just because it works
>
> doesn't mean it is good, efficient or pretty.
>
> I made a copy of my program to try out your
> suggestions. I knew I had to have an integer to use
>
> switch but I couldn't figure out how to turn my
> (oper) string into one, other than the way I did
> it.
>
> I guess I had charAt, indexOf and
> valueOf mixed up in my mind. Not any more.
>
> wonder why there isn't
> static Scanner sc = new
> Scanner(System.in);oper = sc.nextChar
>
> ay, I dug into the docs for charAt, thanks for
> the suggestion, is this what you had in mind?
>
>
>
>
>
>
> switch(oper.charAt(0))
> {
> case 42: System.out.println(num1+" * "+num2+" =
> 2+" = "+dF.format(num1 * num2));
>break;
> case 43: System.out.println(num1+" + "+num2+" =
> 2+" = "+dF.format(num1 + num2));
>break;
> case 45: System.out.println(num1+" - "+num2+" =
> 2+" = "+dF.format(num1 - num2));
>break;
> case 47: System.out.println(num1+" / "+num2+" =
> 2+" = "+dF.format(num1 / num2));
>break;
>
> default: System.out.println("\n\n......
> \n......
>
> Even though I may not be able to use this in my
> assignment;
> everyones comments introduced me to ideas I havent
> come across yet and
> will help me write with (hopefully) more clarity down
> the road.
>
> thanks again
You just gave me hope that not all of the people who are in school today will be useless when thet get out into the workforce.
> Thanks for all the replies. This is exactly the kind of help I was hoping for.
>
> I imagine the constraints are there to help us really understand the concepts at a deep level. But after 3 weeks or so, It is
> dawning on me; that just because it works doesn't mean it is good, efficient or pretty.
Exactly. There are usually many ways to achieve your goal. Some are more efficient, some are prettier, and some are convoluted and hard to understand (you usually don't want those convoluted, hard-to-understand ones).
> I made a copy of my program to try out your suggestions. I knew I had to have an integer to use
>
> switch but I couldn't figure out how to turn my (oper) string into one, other than the way I did it.
The compareTo would work in the switch [I would never have thought of it myself--certainly a novel way to do it :-)], but just like I said, "Use 'equals', because it shows your intent better", using "oper.charAt(0)" shows your intent better.
> I guess I had charAt, indexOf and valueOf mixed up in my mind. Not any more.
>
> wonder why there isn't
> static Scanner sc = new Scanner(System.in);
>oper = sc.nextChar();
>
Not sure why they didn't do that.
> ay, I dug into the docs for charAt, thanks for the suggestion, is this what you had in mind?
>
> switch(oper.charAt(0))
> {
> case 42: System.out.println(num1+" * "+num2+" = "+dF.format(num1 * num2));
>break;
> case 43: System.out.println(num1+" + "+num2+" = "+dF.format(num1 + num2));
>break;
> case 45: System.out.println(num1+" - "+num2+" = "+dF.format(num1 - num2));
>break;
> case 47: System.out.println(num1+" / "+num2+" = "+dF.format(num1 / num2));
>break;
>
> default: System.out.println("\n\n......");
Nice to see you are trying different things, to learn them even if you can't use them in the assignment. But, you can just use the characters in the case statement, to make it even more readable:
switch(oper.charAt(0))
{
case '*': System.out.println(num1+" * "+num2+" = "+dF.format(num1 * num2));
break;
case '+': System.out.println(num1+" + "+num2+" = "+dF.format(num1 + num2));
break;
case '-': System.out.println(num1+" - "+num2+" = "+dF.format(num1 - num2));
break;
case '/': System.out.println(num1+" / "+num2+" = "+dF.format(num1 / num2));
break;
default: System.out.println("\n\n......");
}
MLRona at 2007-7-21 11:01:02 >

Thats cool, I don't even have to look up the ASCII code.Thanks
> Thats cool, I don't even have to look up the ASCII code.You're welcome. Not only do you not have to look it up, but the person reading your code can immediately see what you mean, without having to look up the ASCII code again.
MLRona at 2007-7-21 11:01:02 >

> Not only do you not have to
> look it up, but the person reading your code
> can immediately see what you mean, without having to
> look up the ASCII code again.
Point well taken..... I guess I should always read my code as if I were someone
who had never seen it before.
You know......My wife used to say I was a selfish ******* and that my only concern was ME
I guess she was right ;0)
how weird... I just wrote the word ******* and the some POWER made it into *******, and flounder
said fucking and it printed out fucking. So **** is bad but not fucking or fuckity, fuckitty, fa-q, wonder
what would happen if I spelled it basturd?
yikes, sorry about that,
thanks for the insight MLR
I see you've discovered Sun can't seem to find competent employees to administer forums.
There is nothing wrong with the word bastard.
bastard.... you are going to keep me up all night....
We don't need no steenkin' code tags.If I told you I would lose my GOD-like status over you mere mortals.
> If I told you I would lose my GOD-like status over
> you mere mortals.
If I didn't know you were a programmer before this post sort of confirmed it. And if I had to guess you also have some administrative duties over something which is not usually important (or top of mind anyways) but critical when it fails.
It is really amazing the level of education one can get here.