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

Making a Simple Calculator with Java

I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5

The program needs to compute this and display the result as "The result is: 10"

I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it.

Thanks

This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.  import java.io.*; 
  2. import java.util.*;
  3. public class Calculator
  4. {
  5. public static void main(String[] args)
  6. throws java.io.IOException
  7. {
  8. String I;
  9. double Input;
  10. double Result;
  11. double Interest;
  12.  
  13. InputStreamReader isr = new InputStreamReader(System.in);
  14.  
  15. BufferedReader br = new BufferedReader(isr);
  16.  
  17. System.out.println("What do you want to compute? ");
  18. I = br.readLine();
  19.  
  20. Interest = Double.parseDouble(I);
  21.  
  22. Result = Math(Interest);
  23.  
  24. System.out.println("The result is: " +Result);
  25. }
  26. }
  27.  
Feb 15 '07 #1
15 45095
r035198x
13,262 8TB
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5

The program needs to compute this and display the result as "The result is: 10"

I think my problem comes when I need to tell java to compute the users input. I do not know what function or method I need to use. If anyone can help I would appreciate it.

Thanks

This 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 I;
double Input;
double Result;
double Interest;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

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

Interest = Double.parseDouble(I);

Result = Math(Interest);

System.out.println("The result is: " +Result);
}
}
What operations do you want to be able to perform on your calculator? Just the * or other operations as well. The code you have written is not doing any multiplication so you can't expect it to give you the correct answer yet. And colin, next time you post code, please wrap it around code tags.
Feb 15 '07 #2
I need it to perform addition, subtraction, multiplication, and division.

I think there is a math function that will just calculate whatever input, right? I just cannot find it in my book.

thx,
colin
Feb 15 '07 #3
Actually I might need to use the if command i just feel stuck right now
Feb 15 '07 #4
r035198x
13,262 8TB
I need it to perform addition, subtraction, multiplication, and division.

I think there is a math function that will just calculate whatever input, right? I just cannot find it in my book.

thx,
colin
There is no math function which will just figure it out. That is the function that you want to write.

You will need to be able to split the input String into the operands and the operation first, then use a switch to select which operation to do. Do you think you can write code to split the input String into the operands and operations first?
Feb 15 '07 #5
There is no math function which will just figure it out. That is the function that you want to write.

You will need to be able to split the input String into the operands and the operation first, then use a switch to select which operation to do. Do you think you can write code to split the input String into the operands and operations first?

I am not really sure how to do that. I would have to make it perfom seperate calculations based on what the user inputs. How would I do that? I am a very novice programmer.
Feb 15 '07 #6
r035198x
13,262 8TB
I am not really sure how to do that. I would have to make it perfom seperate calculations based on what the user inputs. How would I do that? I am a very novice programmer.
The operation to perform depends on the operator that has been supplied. If you don't manage to separate the operator from the operands you won't be able to finish this. Have a look at the methods of the String class and chose the methods you think will be required to split a String like "4+5" into three strings, "4", "+", and "5".
Feb 16 '07 #7
DeMan
1,806 1GB
And of course.... it all depends on how simple youmean by simple computation. If you always know two variable and an operand all is goos....if you need to be able to do (slightly) more complex things, look up "Reverse Polish Notation". If you can set up a stack (or two ) in RPN you can do more complex math (though still using the most basic operations)
Feb 16 '07 #8
I found out I need to use the If command.

Here are my instructions
It only expects primitive arithmetic expressions involving two operands connected by one of the operators. +,-,*, /

It only expects non-negative numbers in the input

It assumes that all numbers are real numbers

It assumes that the operands and operators in the user-input expressions are separated by spaces. for example 20 - 15 instead of 20-15


I do not know what to do for the if statements
Feb 17 '07 #9
Ganon11
3,652 Expert 2GB
As r0 said, you should focus first on separating the input into the numbers and the operator.

For now, let's assume you have the user input. Both operators are in integer variables (call them num1 and num2), and the operator is held in a String variable (call it oper). If the user entered a + sign, then oper's contents are "+", and you want to add num1 and num2 for the result. You can use the .equals() method on oper to see if it equals "+", "-", "*", or "/". These will be your if...else branches.
Feb 17 '07 #10
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class Calculator
  4. {
  5. public static void main(String[] args)
  6. throws java.io.IOException
  7. {
  8. float num1;
  9. float oper;
  10. float num2;
  11. double result;
  12.  
  13. InputStreamReader isr = new InputStreamReader(System.in);
  14.  
  15. BufferedReader br = new BufferedReader(isr);
  16.  
  17. System.out.println("What do you want to compute? ");
  18. num1 = br.readLine();
  19. oper = br.readLine();
  20. num2 = br.readLine();
  21.  
  22. if (oper= "*") {result =  num1*num2;}
  23. if(oper = "+") { result = num1 + num2; }
  24. if(oper = "-") { result = num1 - num2; }
  25.  
  26. if(oper = "/") { result = num1 / num2;}
  27.  
  28. System.out.println("The result is: " +result);
  29.  }
  30. }
What am I doing wrong??
Feb 19 '07 #11
nmadct
83 Expert
What am I doing wrong??
You're actually on the right track. Some things I noticed:

- You are reading a whole line of text for each input, but that's not the input that you said you're expecting. Since you can expect that each number and operator is separated by a space, I would try the split method of the String class -- read a line an break it up into tokens (token=meaningful unit of text) that are divided by space characters. Like this: inputLine.split(" "). That's just one way to do it, but I think in this case it's a very convenient way.
- You are trying to do math with string variables. You need to convert them to numbers first. You were doing this in your first version, with this expression: Double.parseDouble(I)

A question, do you need to honor the standard algebraic order of operations, or are you just computing from left to right? That is, which answer to you expect to get here? "3 + 4 * 7 = 31" or "3 + 4 * 7 = 84"? The second one will be easier to do because it doesn't involve any trickery to make sure the right operation is evaluated first.
Feb 19 '07 #12
I do not understand how you got 84. But I dont have toworry about that. I just need to be able to perform one thing at a time. For example 6 + 5 or 10 * 5 instead of multiple operations in one command. It is a very simplecalculator
Feb 19 '07 #13
nmadct
83 Expert
I do not understand how you got 84. But I dont have toworry about that. I just need to be able to perform one thing at a time. For example 6 + 5 or 10 * 5 instead of multiple operations in one command. It is a very simplecalculator
Yeah, ah... you don't get how I got 84 cuz I did it wrong, hehe. It should have been 49, sorry.

Anyway, if the requirement is just simple expressions like that, then the program you have is close to working, assuming you straighten out the issues I mentioned in my last post.
Feb 19 '07 #14
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class Calculator
  4. {
  5. public static void main(String[] args)
  6. throws java.io.IOException
  7. {
  8. float num1;
  9. float oper;
  10. float num2;
  11. double result;
  12.  
  13. InputStreamReader isr = new InputStreamReader(System.in);
  14.  
  15. BufferedReader br = new BufferedReader(isr);
  16.  
  17. System.out.println("What do you want to compute? ");
  18. num1 = br.readLine();
  19. oper = br.readLine();
  20. num2 = br.readLine();
  21.  
  22. if (oper= "*") {result = num1*num2;}
  23. if(oper = "+") { result = num1 + num2; }
  24. if(oper = "-") { result = num1 - num2; }
  25.  
  26. if(oper = "/") { result = num1 / num2;}
  27.  
  28. System.out.println("The result is: " +result);
  29. }
  30. }
What am I doing wrong??
nmadct has given a lot of good pointers for getting this close to the answer. Here are a few more:

when comparing strings, do not use == operator.
Use the .equals method. You can read here for the reason for that. So to you should do something like
if(string.equals("+")) {
//do stuff here
}

Also use an if-else construct for this rather than a series of ifs. Won't notice the difference but you should use a series of ifs only if it is possible that more than one of them can be executed. In this case the operator is only going to be one of +,-,* or / so need for series of ifs
Feb 19 '07 #15
its nice......its good...
Oct 6 '10 #16

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

Similar topics

5
by: bsmith1111 | last post by:
I'm working on a simple calculator similar to the MS Calculator in visual c++ 6.0. I'm trying to teach myself the visual aspect of c++ on my own time, so I've decided to jump right into a program...
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...
1
by: phjones | last post by:
please help resolve some error messages code is compling with errors see below the code. I am new at this please help! /** * @(#)3 Mortgage loans.java * * 3 Mortgage loans application * ...
1
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the...
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...
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,...
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...
6
by: NoviceJava | last post by:
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...
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
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.