Need some help with java

Associate
Joined
26 May 2004
Posts
103
Location
staffordshire
for my uni work i have to create a simple calculator in text oriented java. It needs to take 2 integer values and the following 4 operators + - / * which are all input by the user and outputs a result like 2 + 2 = 4. Im having trouble working out the code to do this is it possible to do this within a switch statement?

any help is appreciated

thanks
 
One of the common ways to implement a calculator is to use a stack and reverse polish notation.
 
Una said:
One of the common ways to implement a calculator is to use a stack and reverse polish notation.

heh, he's asking if he can use a switch statement, I doubt he knows what reverse polish notation is.

What you are essentially creating here is a parser, which, given a statement, will produce a "tree" with the numbers as leaves and the operator as nodes.

Presuming that you know that you're only going to have 2 numbers, and you know there are going to be no more, yada yada....

What you need to do is to scan the string for a number, then an operator and then another number (I would check the javadocs for an appropriate method on a string, probably .explode or something to create an array of chars, then look at them one by one). Load the two numbers into variables, and then depending on the operator (which would be a third variable), add, subtract etc. Yes, you could use a case statement for the operator as well.
 
Personally I would not overcomplicate things.

Your input will be (in Java terms) one long string. So the char array it is using if the console input was 2 + 2 would be "2 + 2"

First you will want to parse the string into seperate components. Take a look at the Strin library definition on the Sun website (hint: subset).

Once you have 3 seperate Strings from your original input I wouldnt use a switch statement myself, I would simply use a If else structure.

So if we have 3 new strings "A, B and C" B will be our operator, A and C will be our values. As you know the operators you are looking for your if else structure would be somethin along the lines of "if(B == "+") { Out = A + C; } else if(B == "-") { Out = A - C; } etc etc :)

I dont want to totally solve it for you or theres no way you can learn :) Java is VERY powerful when it comes to strin manipulation however and I can heartily suggest you get used to using the sun online help (java.sun.com.)

Apologies for any missing "l's" or "g's" in this post, my new Eclipse II keyboard is playin up....

[EDIT] Given your age and location, ae you at Staffordshire University? :) I graduated last year and moved onto manchester. If you are there have you met Peter Hoornaert?
 
Last edited:
If the user input e.g 2+2 is supplied in a single string then another way of splitting it would be to use a regular expression. The regexp could also be used to validate that the input was in the number, operator, number format.

As Una said it would be well worth investigating RPN and stacks. Bit of extra work but it's stuff that's well worth knowing and could make the program easier to code in the long run.
 
ive done this already mate... add me on msn and ill send you my code to look at.

lol your doing isd at staffs i take it?
 
Back
Top Bottom