Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

For loop not starting  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite new to programming and have a problem with this code
I am making a program that is supposed to allow the user to input a number then enter details for that number of products.
The user can then display the details or search for a specific product.
The "searchProd ()" part is not working as it asks for the product name and then never moves onto the for loop it just lets you type infinitely
Also it is compiling and giving me no errorrs so it must be a logical error I'm assuming


 
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your inner loop is causing an infinite loop. You don't need nested loops for this. Only one loop will do the job.
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay the loop starts now but it doesnt totally work

when I enter a product that is not in the list it tells me it is

and when i enter one that is in the list it works



Screenshot-(90).png
[Thumbnail for Screenshot-(90).png]
Entering a product that is not there
 
Carey Brown
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Edward Mallia wrote:okay the loop starts now but it doesnt work at all

Let's see your new code. In general you should re-post your code after you make changes that are causing a new behavior.
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry my bad

all i did was remove the nested do...while loop

 
Carey Brown
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't break out of the loop when you find the product then line 97 will reset searchProd back to false. So unless the product you are searching for is the last one in the productList the result will be false. Also note that you are not initializing searchProd.
 
Ranch Hand
Posts: 548
1
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, assuming you don't have a debugger you can use print statements to debug your code.  For example:

Then, when you know Foo is the problem add more:

Too many prints?  Control them

You can set verbose at compile time, or better yet set it via command line arguments.

Disclaimer:  I come from an embedded background where debuggers often weren't available.  I've turned LEDs on/off to track program flow.  Fun fact:  I got gdb running on an HP Laserjet in '90/91.
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replying to Carey Brown

searchProd is beig initialised on line 85

and i had the exact same thought as you but the problem is that it always comes as true not false so that is why im so confused

i wanted to break out of the loop with that nested do while loop but that seemed to make things worse

Replying to Jim Venolia

I dont see how printing text will work as i know which piece of code is being run the problem is that its the wrong piece of code

the if loop where searchProd is set as true or false doesnt seem to work properly and always sets it to be true

PS i know i can quote to reply i just didnt feel like
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never type if (boolean == true), just type if (boolean) or else you may do this:
Oops!  Always true.
 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likewise, don't type if (boolean == false) but instead type if (!boolean).
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do i do it like this



cause here line 1 gives me an error
 
Knute Snortum
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is searchRes a boolean?  I don't see it in your program.

Also, always post the full error message when you get an error.
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its being initialised on line 85 of the full piece of code

the error is just that its not compiling but when i hover over the error it says variable searchRes might not have been initialised
 
Carey Brown
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 85 **Declares** the variable but it does not initialize it. You need:
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay many things have changed so instead of explaing im just going to add pictures of the results and the new code too obviously

Screenshot-(91).png
[Thumbnail for Screenshot-(91).png]
Screenshot-(92).png
[Thumbnail for Screenshot-(92).png]
Screenshot-(93).png
[Thumbnail for Screenshot-(93).png]
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heres the last pic
Screenshot-(94).png
[Thumbnail for Screenshot-(94).png]
 
Carey Brown
Bartender
Posts: 5817
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Edward Mallia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES it works it was just like you said here except now it was actually initialised and so now it would actually happe

Carey Brown wrote:If you don't break out of the loop when you find the product then line 97 will reset searchProd back to false. So unless the product you are searching for is the last one in the productList the result will be false. Also note that you are not initializing searchProd.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!