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

    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(;
    Scanner scan2 = new Scanner(;
    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
    Jan 2011
    Rep Power


    Read about variable visibility and lifetime ;)

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

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



    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;
    num3 = num1 + num2;

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

  4. #4
    Join Date
    Feb 2011
    Rep Power


    Will do, Thanks!

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

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


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