471,887 Members | 1,496 Online

# Need help with Split String to create a calculator

10
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.showInputDialog("blah blah");
String[] result = inputString.split("//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 4288
Max347
10
Also, ive found references to arrays, so maybe thats my solution? If someone has a guide?
Thanks
Feb 13 '08 #2
r035198x
13,262 8TB
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.print 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.Arrays.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
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.print 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.Arrays.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 8TB
My requirements limit me to the one output..
What do you mean? Surely you can use System.out.println 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
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 8TB
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
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
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 8TB
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.

Feb 13 '08 #10
Max347
10
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.

This is what ive got-

Float.parseFloat(result[0]) = n1;
Float.parseFloat(result[2]) = n2;
switch (1)
{
case '+':
JOptionPane.showMessageDialog(inputString + " = " + n1 + n2);

Will that output 2 + 1 = 3? (I cant run it yet) Thanks
Feb 13 '08 #11
r035198x
13,262 8TB
This is what ive got-

Float.parseFloat(result[0]) = n1;
Float.parseFloat(result[2]) = n2;
switch (1)
{
case '+':
JOptionPane.showMessageDialog(inputString + " = " + n1 + n2);

Will that output 2 + 1 = 3? (I cant run it yet) Thanks
You really do need to go through that tutorial.
You probably wanted to do
Expand|Select|Wrap|Line Numbers
1. float n1 = Float.parseFloat(result[0]);
2. float n2 = Float.parseFloat(result[2]);
3. char c = result[1].chatAt(0);
4. switch(c) {
5. ....
Feb 13 '08 #12
Max347
10

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Calculator{
public static void main (String[] args) {
String inputString = JOptionPane.showInputDialog("Input...etc etc");
String[] result = inputString.split("//s");
for (int x=0; x<result.length; x++)
float n1 = Float.parseFloat(result[0]);
float n2 = Float.parseFloat(result[2]);
char c = result [1].chatAt(0);
switch (c)
{
case '+':
JOptionPane.showMessageDialog(inputString + " = " + n1 + n2);
break;
case '-':
JOptionPane.showMessageDialog(inputString + " = " + n1 - n2);
break;
case '/':
JOptionPane.showMessageDialog(inputString + " = " + n1 / n2);
break;
case '*':
JOptionPane.showMessageDialog(inputString + " = " + n1 * n2);
break;
case '%':
JOptionPane.showMessageDialog(inputString + " = " + n1 % n2);
break;
}
}
}

I really appreciate you taking the time to do this for me, a stranger. thanks, Max
Feb 13 '08 #13
r035198x
13,262 8TB
1.) Use code tags when posting code (like I did above).
2.) Don't just copy the code, try to understand it. (You probably didn't pick that I made a typo in charAt).
3.) What is the for loop for?
4.) There is no showMessageDialog method in JOptionpane which takes one string as argument.
Feb 13 '08 #14
Max347
10
1.) Use code tags when posting code (like I did above).
2.) Don't just copy the code, try to understand it. (You probably didn't pick that I made a typo in charAt).
3.) What is the for loop for?
4.) There is no showMessageDialog method in JOptionpane which takes one string as argument.
Ok, sorry about the code. I took out the loop. What if I just put one output on the bottom? null? When I compile this, it goes through now, asks me for my info, but then doesnt output. Here is what I have-
Expand|Select|Wrap|Line Numbers
1. import javax.swing.*;
2. import java.awt.*;
3. import java.util.*;
4. public class Calculator{
5. public static void main (String[] args) {
6. String inputString = JOptionPane.showInputDialog("Input...etc etc");
7. String[] result = inputString.split("//s");
8.
9. float n1 = Float.parseFloat(result[0]);
10. float n2 = Float.parseFloat(result[2]);
11. char c = result [1].chatAt(0);
12. switch (c)
13. {
14. case '+':
15. JOptionPane.showMessageDialog(null, inputString + " = " + n1 + n2);
16. break;
17. case '-':
18. JOptionPane.showMessageDialog(null, inputString + " = " + n1 - n2);
19. break;
20. case '/':
21. JOptionPane.showMessageDialog(null, inputString + " = " + n1 / n2);
22. break;
23. case '*':
24. JOptionPane.showMessageDialog(null, inputString + " = " + n1 * n2);
25. break;
26. case '%':
27. JOptionPane.showMessageDialog(null, inputString + " = " + n1 % n2);
28. break;
29. }
30. }
31. }
Feb 13 '08 #15
r035198x
13,262 8TB
1.) You still have the typo for charAt
2.) You need to have make use of brackets like this
Expand|Select|Wrap|Line Numbers
1. JOptionPane.showMessageDialog(null, inputString + " = " + (n1 - n2));
3.) What do you hope to match using split("//s")? If you want to match a space then you need to use split("\\s").
4.) All this can be found in that tutorial.
Feb 13 '08 #16
Max347
10
1.) You still have the typo for charAt
2.) You need to have make use of brackets like this
Expand|Select|Wrap|Line Numbers
1. JOptionPane.showMessageDialog(null, inputString + " = " + (n1 - n2));
3.) What do you hope to match using split("//s")? If you want to match a space then you need to use split("\\s").
4.) All this can be found in that tutorial.

PROBLEM SOLVED! You are the master, thank you so much for all your help! Max
Feb 13 '08 #17
r035198x
13,262 8TB
...You are the master, thank you so much for all your help! Max
I am not the master. That tutorial is. Everything I said is in that tutorial.
Feb 13 '08 #18