473,554 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

4
2066
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 and come up with a dollar amount. If anyone out there sees what I am doing wrong- could you offer some advice? I apologize in advance for...
3
2553
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 use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
1
3371
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 suggestion for improvements or conversion to iteration would be much appreciated
6
2864
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 suggestion for improvements or conversion to iteration would be much appreciated
6
7293
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.... #include <stdio.h> #include <stdlib.h>
13
3227
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 happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. ...
2
11103
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 Population Calculator on the javascripkit.com site. The Calculator functions just as I imagine I want my number generator to function, but I need it to...
3
11840
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, my teacher wants the operators to follow the algebraic order of operations by chaining multiple operations. Such as, 7 + 4 * 2= 15. The...
8
2767
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 numbers of the calculation are in fact numbers (isnumeric). the function must check both numbers, display a message box and return a value. but it...
0
7600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7521
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5436
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
841
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.