• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fields won't hold value given through input??  RSS feed

 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alrighty, as you can see at the end of my main method, I want it to print out the input given that should have been stored in my sandwich class fields, but it didn't. I have zero idea why, would anyone like to help me out? sorry if it's unsightly code, I just started programming a couple weeks ago.



 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner.next() is probably not what you want. Try nextLine(). The docs on Scanner are not very clear on this point.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that is really difficult to read and follow. Start by breaking the long lines; I have done most of them, so you can see how it is done. Also follow a common indentation convention. We have one here, and there are others, but I have not seen indentation where the { is farther to the left than the line preceding it.

You told us there are problems about holding values. You didn't say which fields are going wrong. I can see a problem, however, and it has nothing to do with Scanners. You have used the keyword static liberally. As far as I am concerned, if you don't have a good explanation for using static, that is a mistake. If you set the static field in your Bread class to “wholemeal“, then is it “wholemeal“ for every sandwich you have. Imagine you went into the shop and asked for a rye sandwich and the next person asked for wholemeal. The girl behind the counter reaches for a wholemeal loaf and slices wholemeal bread for everybody irrespective of what they requested. You need the bread type to be different for each sandwich, and there should be a bread field for each sandwich.
So delete the keyword static everywhere except after public and before void main(String[] args). See what happens like that.
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though that wasn't a direct answer to the question I had currently I actually really really appreciate the tips you just gave! Thanks Ralph, I'll be sure to keep your words in mind.
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, not Ralph, Ritchie!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!