# Simple Calculator Code in Java - need help please!

Page 1 of 1

## 4 Replies - 43193 Views - Last Post: 18 October 2009 - 10:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=132836&amp;s=3c4869dd92ebc7ceac737d7cae3d0962&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sweetD

Reputation: 0
• Posts: 3
• Joined: 18-October 09

# Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 03:14 PM

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

```import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;

public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
valueB = keyboard.nextInt();
operator = keyboard.next();
operator1 = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;

switch (operatorA) {
case '+':
break;

case '-':
System.out.println(minus);
break;

case '*':
System.out.println(multiply);
break;

case '/':
System.out.println(divide);

case 'C':
System.out.println(clearScreen);
break;

default:
System.out.println("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}
```

Edited by Locke. In the future, .

This post has been edited by Locke: 18 October 2009 - 03:24 PM

Is This A Good Question/Topic? 0

## Replies To: Simple Calculator Code in Java - need help please!

### #2 Tshiknn

Reputation: 4
• Posts: 20
• Joined: 18-October 09

## Re: Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 05:45 PM

sweetD, on 18 Oct, 2009 - 02:14 PM, said:

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

```import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;
public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
valueB = keyboard.nextInt();
operator = keyboard.next();
operator1 = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;

switch (operatorA) {
case '+':
break;

case '-':
System.out.println(minus);
break;

case '*':
System.out.println(multiply);
break;

case '/':
System.out.println(divide);

case 'C':
System.out.println(clearScreen);
break;

default:
System.out.println("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}
```

Edited by Locke. In the future, .

Two things:
1. Your switch statement switches on 'operatorA'. However, you assign the operator character to 'operator1'. That's number 1.
2. Your program appears to run like this:

[code]
Enter problem.10 10 +
20

Enter problem.10 + 10

### #3 sweetD

Reputation: 0
• Posts: 3
• Joined: 18-October 09

## Re: Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 06:16 PM

Thanks for the help. It is now running, however, in order to get the output I have to hit 'enter' after every step - how do I fix the code in order to have the output displayed after you hit enter only once - displaying all the input on one line?

Also, I have the following in the switch statement which is not working
case 'C':
System.out.print(clearScreen);

This is supposed to clear the screen but I'm not sure why it doesn't.

Tshiknn, on 18 Oct, 2009 - 04:45 PM, said:

sweetD, on 18 Oct, 2009 - 02:14 PM, said:

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

```import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;
public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
operator = keyboard.next();
valueB = keyboard.nextInt();
operatorA = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;

switch (operatorA) {
case '+':
break;

case '-':
System.out.print(minus);
break;

case '*':
System.out.print(multiply);
break;

case '/':
System.out.print(divide);

case 'C':
System.out.print(clearScreen);
break;

default:
System.out.print("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}
```

Edited by Locke. In the future, .

Two things:
1. Your switch statement switches on 'operatorA'. However, you assign the operator character to 'operator1'. That's number 1.
2. Your program appears to run like this:

[code]
Enter problem.10 10 +
20

Enter problem.10 + 10

### #4 Tshiknn

Reputation: 4
• Posts: 20
• Joined: 18-October 09

## Re: Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 10:06 PM

sweetD, on 18 Oct, 2009 - 05:16 PM, said:

Thanks for the help. It is now running, however, in order to get the output I have to hit 'enter' after every step - how do I fix the code in order to have the output displayed after you hit enter only once - displaying all the input on one line?

Also, I have the following in the switch statement which is not working
case 'C':
System.out.print(clearScreen);

This is supposed to clear the screen but I'm not sure why it doesn't.

Tshiknn, on 18 Oct, 2009 - 04:45 PM, said:

sweetD, on 18 Oct, 2009 - 02:14 PM, said:

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

```import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;
public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
operator = keyboard.next();
valueB = keyboard.nextInt();
operatorA = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;

switch (operatorA) {
case '+':
break;

case '-':
System.out.print(minus);
break;

case '*':
System.out.print(multiply);
break;

case '/':
System.out.print(divide);

case 'C':
System.out.print(clearScreen);
break;

default:
System.out.print("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}
```

Edited by Locke. In the future, .

Two things:
1. Your switch statement switches on 'operatorA'. However, you assign the operator character to 'operator1'. That's number 1.
2. Your program appears to run like this:

[code]
Enter problem.10 10 +
20

Enter problem.10 + 10

Simply printing null will not clear the screen. Unfortunately, I cannot help you here.

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 10:14 PM

You cannot clear the console in Java. Think of the console as a sheet of paper and the System.out printstream as a permanent marker. In the same way that you cannot erase permanent marker from a sheet of paper, you cannot clear the console since the data has already been written. You could always just output like 20 or so new lines using the "\n" escape sequence. However, the only way to reset the console to absolute blank is to start the program again (or using the paper analogy, get a new sheet of paper).