• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

loop isn't working

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't this loop working in my java code? i want to select more than one item from the list but i'm getting errors while compiling the program here is my program:



it should be working since I added it at line 48
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simply input dosn't have a value yet when it get's to the loop, use a do while instead...
and it's also unclear to me what you're trying to do, my guess is to make this work you'll need to move your menu and the adding of the price inside your loop....
 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)  Learn to indent code properly
2)  If you have a compile error, show us the error message
3)  WAG, as you mention line 48, your curly braces don't line up.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also wouldn't use 2 array's to keep track of the product and it's price but create an object for it
 
Sheriff
Posts: 7111
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
I don't think line 48 will compile.  Try
 
Marshal
Posts: 74381
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Has nobody noticed that line 48 is in a loop all by itself? All that does is keep reading ints from System.in.
If you are trying to avoid a non‑int, that is the wrong way to do it. You wantActually you want a utility class for your keyboard input:-I am afraid you have some very bad design. Parallel arrays are a very bad and non‑object‑oriented design, and very error‑prone because it is easy to get the numbers out of phase. Create a Product class encapsulating price and description. Don't write all the names twice. Iterate the array and print the elements from the array.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Has nobody noticed that line 48 is in a loop all by itself? All that does is keep reading ints from System.in.
If you are trying to avoid a non‑int, that is the wrong way to do it. You wantActually you want a utility class for your keyboard input:-I am afraid you have some very bad design. Parallel arrays are a very bad and non‑object‑oriented design, and very error‑prone because it is easy to get the numbers out of phase. Create a Product class encapsulating price and description. Don't write all the names twice. Iterate the array and print the elements from the array.


Uhm... Yes...I did, I thought that was the loop that wasn't working...

Daniel Demesmaecker wrote:simply input dosn't have a value yet when it get's to the loop, use a do while instead...
and it's also unclear to me what you're trying to do, my guess is to make this work you'll need to move your menu and the adding of the price inside your loop....
I also wouldn't use 2 array's to keep track of the product and it's price but create an object for it

 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote: . . . .... I also wouldn't use 2 array's to keep track of the product and it's price but create an object for it  

Sorry for not noticing that earlier.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic