Results 1 to 5 of 5
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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);
        }
    }
    If you find anthing else of concern, please tell me

    Thanks
    Attached Thumbnails Attached Thumbnails Can't get java calculator to work-calculator-error.png  

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: Can't get java calculator to work

    Thanks for the help, I should be able to figure this out now.

  4. #4
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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);
        }
    }
    Thanks
    Attached Thumbnails Attached Thumbnails Can't get java calculator to work-new-calculator-error.png  

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Can't get java calculator to work

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Did you correct this? I see that your readlines are not exactly the same in each error line.

    Lets look at your first error. temp is already defined in a Java class. How do we get around this problem?
    • 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

  1. java stack RPN calculator
    By ali1 in forum New To Java
    Replies: 0
    Last Post: 10-30-2011, 09:04 PM
  2. actions dont work properly, calculator
    By lordjb in forum New To Java
    Replies: 14
    Last Post: 02-03-2011, 07:00 AM
  3. Getting Backspace on a java calculator program to work
    By marylanddem in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 01:01 AM
  4. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  5. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •