Results 1 to 7 of 7
Thread: Need some really basic help
- 01-24-2009, 04:27 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
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 04:33 AM.
-
What is not working?
What is your specific question?
- 01-24-2009, 04:38 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
I can't get it to display "answer1" because I keep getting an error saying answer1 may not have been initialized.Java Code:System.out.println("Done." + answer1);
-
so why not just do what it's telling you to do -- initialize answer1?
For e.g., where you declare answer1, set it = to "":
The error messages are your friends. Believe them and follow their suggestions and you'll go far.Java Code:String answer1 = "";
- 01-24-2009, 05:51 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
Wow I am an idiot lol, thank you for your help.
-
You're welcome. And remember that we all start out as idiots. and some like me have just have chosen to remain one.
- 01-24-2009, 06:00 AM #7
lol. well, to be more specific, only the top one is your friend. the rest are just there to annoy you.
Originally Posted by Fubarable USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
Basic Java help, AIM?
By jkswebsite in forum New To JavaReplies: 4Last Post: 07-11-2012, 06:17 PM -
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
Basic Applet
By jkswebsite in forum Java AppletsReplies: 4Last Post: 01-13-2008, 09:14 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM -
help with basic example
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks