Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Infinite loop and uneven toString call  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am currently making a basic program, but after a break with Java, I am having significant trouble calling methods in a for loop.

The below code is to iterate (at a user-specified number) through the methods and to also call the toString method printing the results each time. The current loop iterates without stop and the toString only works every second iteration of the loop.

What is going wrong?



Thank you!
 
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd need to see more of the code. Where is 'rund' set? How do you know that its not just a ridiculously large value?
 
Master Rancher
Posts: 3257
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

toString only works every second iteration of the loop.


What does "works" mean? Are you saying the toString method returns different values every other call?  Can you post some sample output?
What is in the Thing class's toString method? 
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code as of now. I have a feeling the scanners are what is throwing the method calls off? Is that likely correct?


 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally, the loop would run without end, and the toString method would print every second iteration. After fiddling with the scanners, now it runs perfectly the first time, then the first and second methods are called consecutively with no tostring output.
 
Carey Brown
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of line 9. you should only ever have one instance of a Scanner created from System.in and the static final one is the correct way to do that.
 
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your toString() method you are calling name(), time(), bags() and pots() again which results in new input being collected.
 
Carey Brown
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 13 is problematic. Scanner#nextInt() will leave a new-line in the Scanner character stream after it retrieves the int. This causes your call to scan.nextLine() in your name() method to think that there's an empty string as input. The solution is to put a call to nextLine() immediately following your call to nextInt(). Just throw away (ignore) any returned string. This will flush the new-line from the input stream.

Ditto for other places you are calling nextInt().
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you very much for such informative assistance! I have deleted the method calls and have only left the which Paweł Baczyński correctly highlighted calls the methods twice. I have also removed the scanner located in Main but I am still getting this result upon running.

Enter number of Histology Specimens
3
Please Enter Patients Lastname, Shortening is OK if Irregular
Brown
Enter Number of Histology Specimen-Bags For This Patient ONLY
3
Enter TOTAL Number of Histo POTS For This Particular Patient
2

Patient Surname - tt
Time of Collection - 22:17
Number of Specimen Bags for This Patient 3
Number of Histo Pots 2

Then upon second iteration, the first and second method are being simultaneously called.

Please Enter Patients Lastname, Shortening is OK if Irregular
Enter Number of Histology Specimen-Bags For This Patient ONLY
 
Carey Brown
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to store your values in class member variables (aka fields). You'll need: name, time, bags, pots.

Then you need to rename your methods so that its intent is obvious.
"name()" for instance, should be more like "inputName()".

Then you need getters for your fields. So you'll need
Then your toString() method should be calling the getters and not the input methods.
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is also after having added scan.nextLine(); after the num=scan.nextInt(); on line 13 as correctly highlighted by Carey Brown.
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh right. Getters and setters, of course. I'll set those up. Thank you!
 
Paweł Baczyński
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly encourage you to change your toString() method so it doesn't collect user input.

Also, can you show your current code?
 
Rancher
Posts: 3595
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Josh Brand wrote:This is also after having added scan.nextLine(); after the num=scan.nextInt(); on line 13 as correctly highlighted by Carey Brown.



You will need to do that everywhere you are using nextInt().
I count 2 more places.
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I have edited, to fit everyone's excellent suggestions. It is working well now. Only issue is the for loop does one too many iterations, but that should be an easy fix. Thanks again, you have all been an amazing help!


 
Carey Brown
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have "setName()", it should be called "getName()".

You currently have no need for setters.
 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You have "setName()", it should be called "getName()".

You currently have no need for setters.



Great, have done that now.
 
Paweł Baczyński
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the condition in the for loop to i < rund.

The standard idiom to iterate n times is
for (int i = 0; i < n; i++) {  }
 
Carey Brown
Bartender
Posts: 4519
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to call time() here, you only need to call it in toString().
 
Marshal
Posts: 5926
411
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the lines 17, 18 supposed to do?
 
Liutauras Vilda
Marshal
Posts: 5926
411
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd personally start over by deleting this code completely and not leaving even as for reference. This is what I normally do when things go wrong.
When you try to fix code which is constructed wrongly from the ground up, you tend to try to accommodate its bad parts with short fixes. This doesn't change the fact that code is still fragile.

For the future though, don't try to write so much code and then start looking whether it works as expected or no. Try to obtain ONLY name. Have toString() method with only name to print. Once you sure it works as expected, move onto second attribute, meaning - bags, then pots...

 
Liutauras Vilda
Marshal
Posts: 5926
411
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try maybe to follow similar template for a start.

 
Josh Brand
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You could try maybe to follow similar template for a start.



Thank you Liutauras Vilda, that is a more organised looking template. I will implement and restructure my code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!