Results 1 to 10 of 10
  1. #1
    Dylex is offline Member
    Join Date
    Sep 2014
    Location
    Hertfordshire, England
    Posts
    5
    Rep Power
    0

    Default Trying to create a calculator (Very new to java)

    I'm new to java and have been coding for a few hours. I tried to create a calculator (Which has no GUI, just text inputs)which will let you select how to process the two integer (Add, minus, times etc.) I cant seem to get it to work. I will put in the process way such as times and have entered two integers and every time it doesn't work.

    Could you explain what i have done wrong and what i need to do to fix it. (Also explaining any new methods or words).

    Thanks in advance.

    Here is my code:

    import java.util.*;

    public class Calculator
    {


    public static void main (String args[])
    {
    int input1 = 0;
    int input2 = 0;
    int answer = 0;
    String method;

    Scanner getinput = new Scanner(System.in);
    System.out.println("This is a calculator, it will let you proccess 2 integers(whole numbers)");
    System.out.println("Please enter whether you want to +, -, * or /. (Please spell it exactly as it is spelt here)");
    method = getinput.nextLine();

    System.out.println("Please type in the first integer you want to " + method);
    System.out.println();
    input1 = getinput.nextInt();

    System.out.println();
    System.out.println("Thankyou, please prepare to enter the second integer you wish to enter");
    System.out.println("Please type in the second integer you want to " + method);
    System.out.println();
    input2 = getinput.nextInt();

    System.out.println();
    System.out.println("Thankyou, the answer will now be calculated");

    if (method == "+")
    {
    answer = input1+input2;
    System.out.println("It has been happening");
    }

    else if (method == "-")
    {
    answer = input1-input2;
    }

    else if (method == "*")
    {
    answer = input1/input2;
    }

    else if (method == "/")
    {
    answer = input1*input2;
    }
    System.out.println("The answer is " + answer);
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Trying to create a calculator (Very new to java)

    When posting code, please use the code tags to preserve formatting.

    What exactly do you mean when you say "it doesn't work"?

    But just by looking at it, one thing that jumps out to me is that you're using == to compare Strings. Don't do that. Use the equals() method instead. Do a search in this forum or on google for an explanation of why, as this is one of the deadest horses in Java and it doesn't need to be beat any more.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Trying to create a calculator (Very new to java)

    You can't compare two Strings for equality with the == operator; use the String.equals( ... ) method instead (read the API documentation for the String class).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Dylex is offline Member
    Join Date
    Sep 2014
    Location
    Hertfordshire, England
    Posts
    5
    Rep Power
    0

    Default Re: Trying to create a calculator (Very new to java)

    The problem was that it return the value 0 everytime i input two numbers. I cant get the string.equals to work either.

    would it be:

    Java Code:
        if (method.equals(+))
            {
                answer = input1+input2;
                System.out.println("It has been happening");
            }
    I get a "Illegal start of expression" error when i try to compile.

  5. #5
    Dylex is offline Member
    Join Date
    Sep 2014
    Location
    Hertfordshire, England
    Posts
    5
    Rep Power
    0

    Default Re: Trying to create a calculator (Very new to java)

    Nevermind, i wrapped "" around the +. Thankyou very much.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Trying to create a calculator (Very new to java)

    Quote Originally Posted by Dylex View Post
    Nevermind, i wrapped "" around the +. Thankyou very much.
    It would be nice to show exactly what you did to solve the problem. Other readers who stumble up on this thread might want to know ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Dylex is offline Member
    Join Date
    Sep 2014
    Location
    Hertfordshire, England
    Posts
    5
    Rep Power
    0

    Default Re: Trying to create a calculator (Very new to java)

    Yes good idea.

    Heres what i did:

    Java Code:
            if (method.equals("+"))
            {
                answer = input1+input2;
                canuseprocess = true;
            }
    You will need a "" when ever you are comparing it to something that isnt another string variable (i think).

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Trying to create a calculator (Very new to java)

    I will just make some statements and hopefully then you will see the error in your ways.

    method -> String variable containing a String value
    + -> an operator, part of the Java language
    "+" -> this is a String literal. In other words: a String object with a String value that just isn't assigned to any variable. This String value has a single character in it, a plus sign

    method.equals(+) -> method equals a Java language operator? What???
    method.equals("+") -> method equals a String literal. Makes perfect sense
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Dylex is offline Member
    Join Date
    Sep 2014
    Location
    Hertfordshire, England
    Posts
    5
    Rep Power
    0

    Default Re: Trying to create a calculator (Very new to java)

    Quote Originally Posted by gimbal2 View Post
    I will just make some statements and hopefully then you will see the error in your ways.

    method -> String variable containing a String value
    + -> an operator, part of the Java language
    "+" -> this is a String literal. In other words: a String object with a String value that just isn't assigned to any variable. This String value has a single character in it, a plus sign

    method.equals(+) -> method equals a Java language operator? What???
    method.equals("+") -> method equals a String literal. Makes perfect sense
    I understand all apart from what you mean with method. Its a string variable, but how is it containing a string variable. Is it because of the name or what?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to create a calculator (Very new to java)

    Quote Originally Posted by Dylex View Post
    I understand all apart from what you mean with method. Its a string variable, but how is it containing a string variable. Is it because of the name or what?
    In Java, anything within paired double quotes (e.g. "someval") is considered a String.

    Regards,
    Jim
    Last edited by jim829; 09-19-2014 at 12:40 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. need help in Calculator java
    By Jmummy in forum New To Java
    Replies: 3
    Last Post: 10-09-2013, 10:37 AM
  2. Java Calculator
    By SilverSoul in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 08:17 PM
  3. Calculator in java
    By andrewparent in forum New To Java
    Replies: 1
    Last Post: 04-20-2012, 09:36 PM
  4. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:01 AM

Tags for this Thread

Posting Permissions

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