Simple Calculator… Yeah Right

By lockefox

First, HUGE thanks to Karel. Without his help, I would not have been able to finish this assignment on time.

So, as I stated in my previous entry, this week’s Java homework was to make a calculator that took user input like “3 + 4″ or “3.7 / 6″ kind of thing. I was having all sorts of trouble with it.

First try:
Tried to implement the program with a method that checked the sign and then passed the value and a true/false value that checked if the numbers were actually numbers.
FAIL

Second try:
Could not get the program to recognize the signs I was putting in. Solution: figure out the ASCII value and use a SWITCH statement to handle the 5 different operations. Next, implement a TRY-CATCH statement to test if the numbers were numbers and life would be good? No. Asked for help from the professor about my program and he responded “No TRY-CATCH. Can’t use those, they haven’t been covered yet”.
SCRAPPED

Third try:
With some input from Karel, I used hasNextDouble() which returns boolean values. If TRUE, then go through the program, if FALSE, program quits before an error can occur. By then using the equals(char) method, I was able to get the whole program to work. Took a last hit of the crowbar to get it to compile correctly, but it works. Even had enough time to do a INT or DOUBLE checker to return even cleaner values.
SUCCESS!

Link to code

~Locke

Leave a Reply