• Post Reply Bookmark Topic Watch Topic
  • New Topic

Infinte loop problem in code  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • 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
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does a change in the while loop?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56529
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!