• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fields won't hold value given through input??

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, not Ralph, Ritchie!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic