Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with an assignment

 
Dan Bolens
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, fixed. :P
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic