473,554 Members | 2,950 Online

# Need help with Split String to create a calculator

10 New Member
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers and an operation, with and output of the answer with the original equation. I think I need to use the split function, however I dont know what to do once it is split. Here is what I have so far-

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Calculator{
public static void main (String[] args){
String inputString = JOptionPane.sho wInputDialog("b lah blah");
String[] result = inputString.spl it("//s");
for (int x=0; x<result.length ; x++)
}
}

I'm new, if you can't tell, so im sorry if this is extremely elementary. In my mind, I would want to give each part of the string a name or letter, then use those for the rest of the code. I dont know if this is possible. Ex- input- "2 + 1" would give a=2 b=+ c=1, then I could go about using if statements to separate the operations (I need +-/* and % (remainder)). Thanks! Max
Feb 13 '08 #1
17 4466
Max347
10 New Member
Also, ive found references to arrays, so maybe thats my solution? If someone has a guide?
Thanks
Feb 13 '08 #2
r035198x
13,262 MVP
Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
1.) Make sure you are getting the input correctly from the user. System.out.prin t it out to the console to test this.
2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Array s.toString for this).
3.) Now you can proceed to do the calculation depending on what the second value in your array is.
Feb 13 '08 #3
Max347
10 New Member
Do it in small steps and satisfy yourself that the current step is correctly done before moving to the next step.
1.) Make sure you are getting the input correctly from the user. System.out.prin t it out to the console to test this.
2.) Make sure your splitting is working as expected. Print out each value in the array to test this.(Can use java.util.Array s.toString for this).
3.) Now you can proceed to do the calculation depending on what the second value in your array is.
My requirements limit me to the one output, would this be easier to tokenize, and make tokens? Thanks!
Feb 13 '08 #4
r035198x
13,262 MVP
My requirements limit me to the one output..
What do you mean? Surely you can use System.out.prin tln statements during your testing. You canthen remove them when you submit the final thing to whoever.

.., would this be easier to tokenize, and make tokens? Thanks!
That's what String.split is doing in your code. It's breaking the input into tokens.
Feb 13 '08 #5
Max347
10 New Member
Oh alright, I gotcha on the outputs. As for the splits, how do I actually use them? Will the result change throughout the code as I use it for different things? What im asking, is can I name them or something? Thanks
Feb 13 '08 #6
r035198x
13,262 MVP
Oh alright, I gotcha on the outputs. As for the splits, how do I actually use them? Will the result change throughout the code as I use it for different things? What im asking, is can I name them or something? Thanks
result is a String array. You can access its elements using
result[0] //first element
result[i]// (i+1)th element
Feb 13 '08 #7
Max347
10 New Member
result is a String array. You can access its elements using
result[0] //first element
result[i]// (i+1)th element
Alright cool, thats exactly what I was looking for.
Ok, I put in the String a = a, etc, however it says it is not a statement.

String a = result[0];
String b = result[1];
String c = result[2];
switch (b)
{
case +:
case -:
case /:
case *:

I know I need to fill in each possible case, but is it right? The complier is telling me I can't do the String a = result[x]; lines. Also, do I need anything around each operation? Thanks

Im sorry in advance is this stuff is really stupid, its my second week in the class and the book is unclear. Thanks!
Feb 13 '08 #8
Max347
10 New Member
Ok, ive been experimenting, and took out the whole String a, String b business, I guess they dont need to specifically be defined. I now still have the "illegal start of expression on the colons in my cases. Am I missing something? Thanks
Feb 13 '08 #9
r035198x
13,262 MVP
Ok, ive been experimenting, and took out the whole String a, String b business, I guess they dont need to specifically be defined. I now still have the "illegal start of expression on the colons in my cases. Am I missing something? Thanks
You can't switch on Strings.
If you want to use a switch then you need to change the result[1] value to a char(you can switch on chars). Otherwise use if-else statements.

P.S Use Sun's Java tutorial as your reference. You can even download the whole thing to your computer.
Feb 13 '08 #10