Results 1 to 7 of 7
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default Need some help with if statement

    Ok well I have to write a program for class and what I'm trying to do is get the value of a variable from an if statement and display that value. I just entered in the variable in the last system.out line where it says "Done" + answer1.

    Any help at all would be greatly appreciated.

    Java Code:
            if( operator1.equals("+") ) {
                // operator was: +
                answer1 = number1 + number2;
                System.out.println("That gives: " + answer1);
            }
            
            else if( operator1.equals("-") ) {
                // operator was: -
                answer1 = number1 - number2;
                System.out.println("That gives: " + answer1);
            }
            
            else if( operator1.equals("*") ) {
                // operator was: *
                answer1 = number1 * number2;
                System.out.println("That gives: " + answer1);
            }
            
            else if( operator1.equals("/") ) {
                // operator was: /
                answer1 = number1 / number2;
                System.out.println("That gives: " + answer1);
            }
            else {
                System.out.println("You have entered an invalid operator.");
            }
            
            System.out.println("Done." + answer1);
    Last edited by Mayur; 01-24-2009 at 05:33 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What is not working?

    What is your specific question?

  3. #3
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    System.out.println("Done." + answer1);
    I can't get it to display "answer1" because I keep getting an error saying answer1 may not have been initialized.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    so why not just do what it's telling you to do -- initialize answer1?

    For e.g., where you declare answer1, set it = to "":
    Java Code:
    String answer1 = "";
    The error messages are your friends. Believe them and follow their suggestions and you'll go far.

  5. #5
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Wow I am an idiot lol, thank you for your help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. And remember that we all start out as idiots. and some like me have just have chosen to remain one.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable
    The error messages are your friends. Believe them and follow their suggestions and you'll go far.
    lol. well, to be more specific, only the top one is your friend. the rest are just there to annoy you.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 07:17 PM
  2. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  3. Basic Applet
    By jkswebsite in forum Java Applets
    Replies: 4
    Last Post: 01-13-2008, 10:14 PM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 07:30 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 06:45 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
  •