473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 4453
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.

P.S Use Sun's Java tutorial as your reference. You can even download the whole thing to your computer.
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.

P.S Use Sun's Java tutorial as your reference. You can even download the whole thing to your computer.
What about floats? how about this-
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
What about floats? how about this-
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
Yeah, I know I do. Im downloading it right now (thanks for the link btw). Here's what I have-

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

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kim14 | last post by:
I have a table that works fine in IE, but doesn't work in Netscape or Firefox. It should automatically come up with numbers in some of the fields and depending what is entered, it should calculate...
3
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
1
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
2
by: masker | last post by:
I was on the web trying to find a javascript that would allow me to input a number on my website and have it increase by a given percentage every second. During my search I found the Earth...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
8
by: jac130 | last post by:
I need to create a calculator using combo boxes/procedures/functions etc... and some other stuff. most of it works properly, but, i need to create a user defined function that checks if both input...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.