4 Replies - 32600 Views - Last Post: 18 October 2009 - 10:14 PM Rate Topic: -----

#1 sweetD  Icon User is offline

  • New D.I.C Head

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

Simple Calculator Code in Java - need help please!

Post icon  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 '+':
System.out.println(add);
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, :code:.

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  Icon User is offline

  • New D.I.C Head

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

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

Posted 18 October 2009 - 05:45 PM

View PostsweetD, 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 '+':
System.out.println(add);
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, :code:.


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
Exception in thread "main" java.util.InputMismatchError
Was This Post Helpful? 0
  • +
  • -

#3 sweetD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

View PostTshiknn, on 18 Oct, 2009 - 04:45 PM, said:

View PostsweetD, 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 '+':
System.out.print(add);
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, :code:.


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
Exception in thread "main" java.util.InputMismatchError

Was This Post Helpful? 0
  • +
  • -

#4 Tshiknn  Icon User is offline

  • New D.I.C Head

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

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

Posted 18 October 2009 - 10:06 PM

View PostsweetD, 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.

View PostTshiknn, on 18 Oct, 2009 - 04:45 PM, said:

View PostsweetD, 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 '+':
System.out.print(add);
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, :code:.


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
Exception in thread "main" java.util.InputMismatchError


Simply printing null will not clear the screen. Unfortunately, I cannot help you here.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • 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).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1