Win a copy of Fixing your Scrum this week in the Agile 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Infinte loop problem in code

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi this is my code




when I run this code this will go to infinte loop,where is the bug?


thanks
 
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
Where does a change in the while loop?
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The condition for the while loop is that (a != 1). the variable a is set before the loop starts by reading from the file. There is nothing inside the loop to tell it to stop and read more input from the file. Whatever the single int value is it read from the file, that is what it keeps using in the condition.

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I did change on

while(a!=-1)

to

while((a=is.read())-1)

and it works fine.Now there is not problem of infinite loop
 
Marshal
Posts: 73732
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on earth are you using read()? A dreadful method. I hope it is only so you can see how it works, or maybe so you can see how it doesn't work.
If you really want to read a text file, don't use an input stream. Use a file reader and wrap it in a buffered reader. Or, better, a Scanner. Or better still, NIO2.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner in java i/o.So I am learning all these stuff.

Sure for future refrence I will use Reader class

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

Kishor Joshi wrote: . . . I will use Reader class

Thanks

You're welcome

But start by going through the Java® Tutorials, as I gave you a link to earlier. You may end up with strange syntax like this:-It looks strange but it is an effiecient way to read every line in a text file with a Buffered Reader.
 
reply
    Bookmark Topic Watch Topic
  • New Topic