Results 1 to 5 of 5
- 12-01-2011, 05:30 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Can't get java calculator to work
Hi,
I am trying to write a calculator application that when it works, it would ask the user what operation they would like, then would perform the operation on two inputed numbers (e.x. input: add, ,input: 1, input: 2, output: 3). so far, I havent been able to get the program to compile because I get 5 error messages regarding the switch statement (I have attached a screenshot of the error messages) when I try to compile. I can't continue writing the code until I can resolve this issue. here is the code:
Java Code:import java.io.*; import java.util.*; import java.text.*; public class calculator{ public static void main(String []args) throws IOException{ System.out.println("Input operation:"); //ask for operation int operation; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int temp; do { int temp = in.readLine(); operation = Integer.valueOf(temp).intValue(); switch(operation) { case 1: System.out.println(" "); System.out.println("First number:"); double num1 = in.readline(); System.out.println(" "); System.out.println("Second number:"); double num2 = in.readline(); double product = num1 + num2; System.out.println(" "); System.out.println("Product:"); System.out.println(product); } } while(true); } }
Thanks
- 12-01-2011, 05:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Can't get java calculator to work
The method readLine() returns a String representing the line that was just read; you are trying to assign that String to an int or a double. b.t.w. note the capital ell (L) in 'readLine'
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-01-2011, 06:58 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: Can't get java calculator to work
Thanks for the help, I should be able to figure this out now.
- 12-02-2011, 04:20 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: Can't get java calculator to work
I tried what you said, but it didn't work. I got a new error (which i attached to this post) and I still don't know what to do. Can someone help?
here is what i tried:
Java Code:import java.io.*; import java.util.*; import java.text.*; public class calculator{ public static void main(String []args) throws IOException{ System.out.println("Input operation:"); //ask for operation String operation; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String temp; do { String temp = in.readLine(); operation = String.valueOf(temp).StringValue(); switch(operation) { case 1: System.out.println(" "); System.out.println("First number:"); double num1 = in.readline(); System.out.println(" "); System.out.println("Second number:"); double num2 = in.readline(); double product = num1 + num2; System.out.println(" "); System.out.println("Product:"); System.out.println(product); } } while(true); } }
- 12-02-2011, 04:38 PM #5
Re: Can't get java calculator to work
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
java stack RPN calculator
By ali1 in forum New To JavaReplies: 0Last Post: 10-30-2011, 10:04 PM -
actions dont work properly, calculator
By lordjb in forum New To JavaReplies: 14Last Post: 02-03-2011, 08:00 AM -
Getting Backspace on a java calculator program to work
By marylanddem in forum New To JavaReplies: 1Last Post: 12-09-2010, 02:01 AM -
Java Calculator
By aapanju in forum New To JavaReplies: 3Last Post: 04-17-2008, 06:33 AM -
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04-16-2008, 02:19 PM
Bookmarks