473,397 Members | 1,985 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,397 software developers and data experts.

Very Simple Calculator Program Help

I'm new to JAVA and need some help writing a simple calculator program.

Here are the instructions:

-expects espression with 2 operands together with either +, -, *, or / operator
-espects only positive input
-assumes all real #'s
-assumes operands and operators in expression are separated with spaces

here is what I have so far:

import java.io.*;
import java.util.*;
public class Calculator
{
public static void main(String[] args)
throws java.io.IOException
{
String inputString, s1, s2, s3;
float num1, num2, num3, result;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("What do you want to compute? ");
inputString = br.readLine();

StringTokenizer st = new StringTokenizer(inputString);

s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();

num1 = Float.parseFloat(s1);
num2 = Float.parseFloat(s3);

if (s2.equals("+"))
result = (num1 + num2);
else if (s2.equals("-"))
result = (num1 - num2);
else if (s2.equals("/"))
result = (num1 / num2);
else if (s2.equals("*"))
result = (num1 * num2);

System.out.println("The result is " + result);
}
}
Mar 1 '08 #1
6 3463
Stwange
126 Expert 100+
What have you tried so far, what is it doing and what were you expecting it to do? You can't just post the assignment here along with the contributed code and expect us to fill the blanks in for you.
Make an effort, and I'll be happy to help you if you go wrong.
Mar 1 '08 #2
What have you tried so far, what is it doing and what were you expecting it to do? You can't just post the assignment here along with the contributed code and expect us to fill the blanks in for you.
Make an effort, and I'll be happy to help you if you go wrong.
I'm not sure what you mean. The code above is what I have worked on so far. When I try to compile it I get an error message saying that the variable result may not have been initialized.

I am expecting it to read "What do you want to compute," whereby I need to input the expression in the required format mentioned previously and for it to give me the result after reading "The result is." The expression only needs to involve two operands. I've tried many different things and I think what I have now is the best of what I have tried thus far; I read a similiar post and it mentioned that the tokenizer should be used so I incorporated it into the source code.
Mar 1 '08 #3
Laharl
849 Expert 512MB
Your compiler is complaining about result because you only assign it values in your if statements. As such, it is possible for result to reach the System.out.println() call without having an explicit value. To fix this, simply set it to zero (or some other default value) when you declare it.

Also, StringTokenizer has been deprecated as of 1.6 or maybe 1.5. Use the String .split() method instead.
Mar 1 '08 #4
sukatoa
539 512MB
I'm new to JAVA and need some help writing a simple calculator program.

Here are the instructions:

-expects espression with 2 operands together with either +, -, *, or / operator
-espects only positive input
-assumes all real #'s
-assumes operands and operators in expression are separated with spaces

here is what I have so far:

import java.io.*;
import java.util.*;
public class Calculator
{
public static void main(String[] args)
throws java.io.IOException
{
String inputString, s1, s2, s3;
float num1, num2, num3, result;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("What do you want to compute? ");
inputString = br.readLine();

StringTokenizer st = new StringTokenizer(inputString);

s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();

num1 = Float.parseFloat(s1);
num2 = Float.parseFloat(s3);

if (s2.equals("+"))
result = (num1 + num2);
else if (s2.equals("-"))
result = (num1 - num2);
else if (s2.equals("/"))
result = (num1 / num2);
else if (s2.equals("*"))
result = (num1 * num2);

System.out.println("The result is " + result);
}
}
Maybe you can get some idea in this
thread...

I hope it helps,
sukatoa
Mar 1 '08 #5
Your compiler is complaining about result because you only assign it values in your if statements. As such, it is possible for result to reach the System.out.println() call without having an explicit value. To fix this, simply set it to zero (or some other default value) when you declare it.

Also, StringTokenizer has been deprecated as of 1.6 or maybe 1.5. Use the String .split() method instead.
Thanks, this worked.
Mar 1 '08 #6
Happy to hear you solve your problem.
Mar 2 '08 #7

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

Similar topics

17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
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.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
1
by: Bl00dFox | last post by:
Hi I am making a simple program to calculate interest. At the beginning when the user has to pick 1 or 2 (to select simple or compound interest respectively), if the user enters a letter (eg, a)...
2
by: JasmynGomez | last post by:
This was an assignment for our JAVA class I am still a beginner and my code keeps getting errors, and I am so confused. I've tried every different way I can think of, can someone please take a...
2
by: moon123 | last post by:
We have to program it in two parts: the lexical analyzer and the syntax analyzer plus semantic.The lexical analyzer should recognize the token using DFA and return the token and the value. The syntax...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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,...
0
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,...
0
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...
0
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...

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.