This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help with an assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with an assignment" Watch "Need help with an assignment" New topic
Author

Need help with an assignment

Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Hey Coders!

I'm working on a project for school, and I don't wan't an answer, but more of a hint to figure out what to do next. The goal is to have the user input "R" or "C" and then enter a length/width, and get an output of that along with some other things (for two pizzas). I cant figure out why my program just terminates after the initial input. The first part of my code is:

Tyler Kenna
Greenhorn

Joined: Sep 08, 2012
Posts: 7
Hi Dan,

So you are dealing with an issue of scope. As you said, because the variables are declared withing the {} braces, they are destroyed when the if statement completes. The fix is simple. Move the declaration of your variables outside the if statement. You can then assign them inside the if statement when the time comes. i.e.



Hope that helps!

Aeiouy

Edit: Oops, looks like you changed your question a bit while I was responding.
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Tyler Kenna wrote:Hi Dan,

So you are dealing with an issue of scope. As you said, because the variables are declared withing the {} braces, they are destroyed when the if statement completes. The fix is simple. Move the declaration of your variables outside the if statement. You can then assign them inside the if statement when the time comes. i.e.



Hope that helps!

Aeiouy


I just worked around this first problem for now, but I may go back and rework it with this. I now have a problem with my if/else if statements as outlined above. :P
Tyler Kenna
Greenhorn

Joined: Sep 08, 2012
Posts: 7
Hi again,
So in response to your newest question, the reason your if statement is not working properly is because of the way the == sign works. When you use the == sign, you are comparing whether two strings are the same object. The rule to remember is:

== tests for reference equality.
equals() tests for value equality.

So when you say if(first_pizza == "R") what you are really saying is to compare whether they are the same object, not the same value. If you want to compare the string values, the best way to do that would be with the .equals() method. Javadocs can help you with how to use that method.

Hope that helps!

Aeiouy
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Tyler Kenna wrote:Hi again,
So in response to your newest question, the reason your if statement is not working properly is because of the way the == sign works. When you use the == sign, you are comparing whether two strings are the same object. The rule to remember is:

== tests for reference equality.
equals() tests for value equality.

So when you say if(first_pizza == "R") what you are really saying is to compare whether they are the same object, not the same value. If you want to compare the string values, the best way to do that would be with the .equals() method. Javadocs can help you with how to use that method.

Hope that helps!

Aeiouy


Yes! This is exactly what I needed and my program now works! Thanks for your help.
Dan Bolens
Greenhorn

Joined: Sep 10, 2012
Posts: 25
Nevermind, fixed. :P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with an assignment
 
Similar Threads
Erroneous sym type What does this mean?
[HOMEWORK] Please need help with java project
toString() method
Large Numbers
math problems as jnlp