Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Unhappy [Question]Simple Calculator problem?

    I'm an amateur Java programmer.
    Started learning, but then had some issues, and proceeded to learn Flash. Flash isn't exactly what I'm looking for, so I'm trying Java again. I have a problem with a calculator I'm attempting to make.
    I was wondering if you could help me point out the flaw(s) in my calculator... THe source code is as follows, and it's pretty basic. I just can't get it to display the final output number...

    import java.util.Scanner;


    public class SimpleCalculator {


    public static void main(String[] args) {
    // TODO: Input/Output integers - only 1 scanner variable needed


    System.out.println("Please enter a number below, can be decimal or whole");
    Scanner scan = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);
    double num1 = scan.nextDouble();

    System.out.println("Number one equals "+ num1);
    System.out.println("Please enter a second number below");

    double num2 = scan.nextDouble();
    System.out.println("Number two equals "+ num2);

    System.out.println("Please enter an operator for these numbers: +,-,*,/");
    String oper = scan2.nextLine();

    System.out.println("Your chosen operator is " + oper);

    if (oper == "/")
    {double num3 = num1/num2;}
    else if (oper == "+")
    {double num3 = num1+num2;}
    else if (oper == "-")
    {double num3 =num1 - num2;}
    else if (oper == "*")
    {double num3 = num1*num2;}

    System.out.println("The finalized output for your numbers are: " + num3);
    }
    }
    It always says, in the last line, num3 cannot be resolved to a variable.

    I just can't seem to get this to work, I've tried setting double num3 = 0; then running the code, I've also tried removing it completely...

    Any help would be gladly appreciated, thanks, Joe.

  2. #2
    mastal is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Read about variable visibility and lifetime ;)

    Also check how to compare Strings in Java.
    Last edited by mastal; 02-01-2011 at 10:15 PM.

  3. #3
    ace84 is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Hey,

    instead of testing a value with the '=' sign, use the .equals comparison instead. Also declare 'num3' before the if statement.

    For example:
    double num3 = 0;

    if( oper.equals("-" ) )
    num3 = num1 - num2;
    else if( oper.equals("*" ) )
    num3 = num1 * num2;
    else if( oper.equals("/" ) )
    num3 = num1 / num2;
    else
    num3 = num1 + num2;

    that should give you the correct results.
    Last edited by ace84; 02-01-2011 at 10:16 PM.

  4. #4
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default

    @Mastal
    Will do, Thanks!

    @Ace84
    That worked! Thanks! Two hours trying to fix this, solved in a matter of minutes. Thanks again!
    ~Joe

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you don't understand/didn't find the explanation of what caused your problem here is a quick one:

    The double num3 is declared inside of curly braces, and thus it has a scope of only inside the curly braces, once the braces are exited, the declared variable no long exists.

Similar Threads

  1. Simple Calculator
    By marksey07 in forum New To Java
    Replies: 12
    Last Post: 01-09-2011, 07:30 AM
  2. Re: Simple Calculator cont'd
    By JavaHater in forum New To Java
    Replies: 3
    Last Post: 01-07-2011, 03:00 AM
  3. creating a simple calculator
    By hobo in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 04:09 AM
  4. Simple Calculator Display Problem :(
    By jimbob in forum Java Applets
    Replies: 4
    Last Post: 07-18-2009, 05:13 AM
  5. [SOLVED] Simple Conversion Calculator
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 02:06 AM

Posting Permissions

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