473,766 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making a Simple Calculator with Java

22 New Member
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 45143
r035198x
13,262 MVP
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.IOExcep tion
{
String I;
double Input;
double Result;
double Interest;

InputStreamRead er isr = new InputStreamRead er(System.in);

BufferedReader br = new BufferedReader( isr);

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

Interest = Double.parseDou ble(I);

Result = Math(Interest);

System.out.prin tln("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
colinNeedsJavaHelp
22 New Member
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
colinNeedsJavaHelp
22 New Member
Actually I might need to use the if command i just feel stuck right now
Feb 15 '07 #4
r035198x
13,262 MVP
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
colinNeedsJavaHelp
22 New Member
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 MVP
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 Top Contributor
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
colinNeedsJavaHelp
22 New Member
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 Recognized Expert Specialist
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

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

Similar topics

5
2477
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 after reading through a lot of material that has gotten me familiar with it. I have a result edit box at the top of the window which holds whatever is being typed in via the numerical buttons and arithmetic buttons (+, -, *, /). Each numerical...
3
15143
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 calculator. Ive made 4 classes 2 do this, my reasoning so it was easier to look through( when programming) rather than getting mixed up in my own code! My questions and problems: Ive been messing around with the windows look and feel,...
24
6341
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
1
9816
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 * * Phyllis J Jones * Purpose to write a program in Java without a graphical user interface and have
1
6841
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 following error message: --------------------Configuration: <Default>-------------------- C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\mortgageloan\src\mortgageloan.java:30: operator > cannot be applied to double,int ...
19
4031
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 java.awt.*; import java.awt.event.*; import java.io.*;
3
11853
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 operatorListener is the ActionListener for the operator buttons. Thanks for any help you can give me. ...
2
6484
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 look and point out some of the errors. Even just point me to the right direction. I've been working on this for 3 days. HELP!!!!! Write a Geometry Calculator that computes the perimeter and area of the following objects: (a) Trapezoid (general...
6
3491
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 positive input -assumes all real #'s -assumes operands and operators in expression are separated with spaces here is what I have so far:
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3929
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.