# Calculator Program

Page 1 of 1

## 6 Replies - 136816 Views - Last Post: 13 April 2008 - 07:53 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=19521&amp;s=3af76865e22faa849cf6c6ba5d2adfc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 todd3428

Reputation: 1
• Posts: 10
• Joined: 10-October 06

# Calculator Program

Posted 10 October 2006 - 04:43 PM

I need help figuring out what type of loops/if statements to be using. I am in a beginning programming class so much of the code used for the mortgage calculators we havent learned yet. Here is our assignment:

Write a calculator program that says welcome to the calculator. It then asks if you want to multiply, divide, add, or subtract. Then once you get into the multiply, divide, add, or subtract loop it will do the calculation output the answer and then ask if you want to do another calculation or exit the program.

Example
Welcome to the calculator!
To multiply use m, to divide use d, to add use a, and to subtract use s

Input first number
Input second number
You chose to multiply. The product is 20
Do you want another calculation (y/n)?

Here is my code so far:

```import java.util.Scanner;

public class calculator

{
public static void main(String[] args)
{
String input;
int firstnumber;
int secondnumber;

System.out.println ("Welcome to the calculator!");
System.out.println ("To multiply use m, to divide use d, to add use a, to subtract use s");

Scanner keyboard = new Scanner(System.in);

do
{
System.out.println ("Do you want to multiply, divide, add, or subtract?  ");
keyboard.nextLine();
input = keyboard.nextLine();

System.out.println ("Input first number:  ");
keyboard.nextLine();
input = keyboard.nextLine();
firstnumber = keyboard.nextInt();

System.out.println ("Would you like another calculation (y/n)?   ");
keyboard.nextLine();
input = keyboard.nextLine();
}
System.exit(0);
}
}
```

Is This A Good Question/Topic? 1

## Replies To: Calculator Program

### #2 ProGraM

Reputation: 10
• Posts: 150
• Joined: 28-September 05

## Re: Calculator Program

Posted 10 October 2006 - 05:21 PM

this is a version of your code using BufferReader and InputReader. personally i like using this way more then the scanner. Also the Integer.parseInt converts an int to a string you can also do this for a double and etc... If you still wish to use scanner u can change the program up but i beleive this is the best way. Enjoy!
```import java.io.*;
import java.util.*;
public class calculator

{
public static void main(String[] args) throws IOException
{
String input, input1, input2;

System.out.println ("\t\t\tWelcome to the calculator!");
do
{
System.out.print ("\nWould you like to Multiply(m), Divide(d), Subtract(s), Add(a)?: ");
if (input.equals("m"))
{
System.out.print ("Please enter the first number!: ");
num1 = Integer.parseInt (input1 = keyboard.readLine());
System.out.print ("Please enter the second number!: ");
num2 = Integer.parseInt (input2 = keyboard.readLine());
System.out.print ("You chose to multiply."+"\t"+"The Product is: "+answer);
}
if (input.equals("d"))
{
System.out.print ("Please enter the first number!: ");
num1 = Integer.parseInt (input1 = keyboard.readLine());
System.out.print ("Please enter the second number!: ");
num2 = Integer.parseInt (input2 = keyboard.readLine());
System.out.print ("You chose to divide."+"\t"+"The Dividend is: "+answer);
}
if (input.equals("a"))
{
System.out.print ("Please enter the first number!: ");
num1 = Integer.parseInt (input1 = keyboard.readLine());
System.out.print ("Please enter the second number!: ");
num2 = Integer.parseInt (input2 = keyboard.readLine());
}
if (input.equals("s"))
{
System.out.print ("Please enter the first number!: ");
num1 = Integer.parseInt (input1 = keyboard.readLine());
System.out.print ("Please enter the second number!: ");
num2 = Integer.parseInt (input2 = keyboard.readLine());
System.out.print ("You chose to subtract."+"\t"+"The Diffrence is: "+answer);
}

System.out.print ("\nWould you like another calculation (y/n)?: ");
}
while(input.equals("y"));
if(input.equals("n"));
System.exit(0);
}
}

```

### #3 chachejave

Reputation: 0
• Posts: 4
• Joined: 24-February 08

## Re: Calculator Program

Posted 24 February 2008 - 06:05 PM

I am taking my first computer programming class in many many years. This helps a lot to see how another student and a programmer approached the problem that I am working on. And thanks for the reminder that its not your job to do my homework.
chachejave

### #4 Mastergeek666

Reputation: 1
• Posts: 151
• Joined: 10-August 07

## Re: Calculator Program

Posted 24 February 2008 - 07:14 PM

First of all you need to put your code in a tag => [code ]code here[ /code], next you'd be better off using a BufferedReader to recognize input. For example,
```import java.io.*;
public class Input {

public static void main(String[] args) throws IOException // have to throw IOException unless you wanna do try catch
{
String input; // your input variable
input = keyboard.readLine(); // this line allows for input
}
}

```

Then when you wanna use that input for checking just call input and sense your making a calculator, you gonna need to parse the input so that you can use it as a int instead of a string. Or you can just make the input parsed into an int as they input, like so
```int first, second, answer;
first = Integer.parseInteger(input = keyboard.readLine()); // parses input into int

```

That was just an example of multiplication of the two inputs. You could have the first input to check what symbol they picked(a,s,m,d) then use an if statement to check which they selected then do which ever math they picked.

This post has been edited by Mastergeek666: 24 February 2008 - 07:14 PM

### #5 Nuusiku

Reputation: 0
• Posts: 2
• Joined: 12-April 08

## Re: Calculator Program

Posted 12 April 2008 - 03:58 AM

Thanx for you help, but you see there is another part of the calculator that i dont understand it should be like in diffrent classes that i know but we should use an interface which i can not do, can u please help me with this?

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Calculator Program

Posted 13 April 2008 - 12:26 PM

Nuusiku, on 12 Apr, 2008 - 03:58 AM, said:

should be like in diffrent classes that i know but we should use an interface

Ouf... not easy to understand

Can you post textually your assigment text ?

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Calculator Program

Posted 13 April 2008 - 07:53 PM

pbl, on 13 Apr, 2008 - 12:26 PM, said:

Nuusiku, on 12 Apr, 2008 - 03:58 AM, said:

should be like in diffrent classes that i know but we should use an interface

Ouf... not easy to understand

Can you post textually your assigment text ?

6.676 visits for this topic....
A lot of students must have a Calculator assigment :-)