This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop with an if loop (help)

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am getting the output correctly for this program, but I am not able to terminate the loop. It keepssssss printimgg. How can I terminate the loop?

 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char symbol;
int periodCt = 0;
int commaCt = 0;
int questionCt = 0;
int colonCt = 0;
int semicolonCt = 0;
symbol =(char) inFile.read();
while(symbol !=-1) {
if(symbol == ';')
semicolonCt++;
symbol = (char)inFile.read();
System.out.println("the semi colons is " +semicolonCt); }

Ok I'm not an expert, so maybe someone else has a better perspective; however, I don't see that symbol will ever be equal to -1; therefore, the while statement will not end.
while(symbol !=-1) {
if(symbol == ';')
semicolonCt++;

symbol =(char) inFile.read(); Why are you using the twice?

Like I said, I'm no expert just giving my opinion; therefore, I too will check back to see what the more qualified programmers have to say.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that -1 is used as an end-of-file marker is because it's not a valid value for the "char" data type. If you look, you'll see that even though read() returns a character, its return type is actually "int", not "char". If you use an int to get the result of read, then you can test it against -1 correctly and your loop will stop. An int can hold every valid char value, plus it can also hold -1. You can still cast to char.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's probably a better way, but I think this would work...

The read method returns an int, which is the value you want to test for an end-of-file -1. However, this is not within the range of a char, which can only be positive. So the above code gets the int, tests it, and then casts it to type char if all is well.
 
Henry Wong
author
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hmmm... I am wondering why Java isn't complaining about the (lack of a) cast. Is it because Java is promoting symbol from a char to an int, which doesn't have to be explicit?

Henry
[ April 26, 2005: Message edited by: Henry Wong ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tonia: The inFile.read() method is first called outside of the while loop in order to establish an initial value (used as the loop's test condition). Then it's also included inside the loop in order to keep getting the next value (until the test fails).
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is terminating once I cast it .But after many lines.
I tried to place '\n' in while loop like
----------------------------------
while(symbol != '\n')
---------------------------------------
I get the same problem, the loop is not terminating. The loop should terminate once the it checks all new lines n there are no more semicolons.

---------------------------------------------------------------------------
My shell1.dat file contains :

There are lots of symbols in this file: ..;;??
.,.,.,?::: .

---------------------------------------------------------------------------
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prerna boja:
... It is terminating once I cast it. But after many lines...

Your loop is executing a println every time a char is read from the file. If you only want it to output when it finds a new semicolon, then put your println inside a block following the if statement...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tonia Billiot:

Ok I'm not an expert, so maybe someone else has a better perspective; however, I don't see that symbol will ever be equal to -1; therefore, the while statement will not end.
while(symbol !=-1) {
if(symbol == ';')
semicolonCt++;

symbol =(char) inFile.read(); Why are you using the twice?

Like I said, I'm no expert just giving my opinion; therefore, I too will check back to see what the more qualified programmers have to say.


Calling inFile.read() twice like this is a very common practice. Notice that the first call is just before the loop starts. This ensures that symbol has a valid value for the test at the beginning of the while loop. The inFile.read() call at the end of the loop ensures that the next character is read from the file just before the loop repeats and the test is executed again.

Layne
 
Moritz Petersen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just wrote an article about the looping topic. You can read / review it here:

http://moritzpetersen.de/index.php/archives/2005/04/27/more-on-java-loops/


It explains how to avoid common problems in loops like duplicate code (as for conditions and assignments) and for reducing the scope of the loop variable.


Any comments are welcome, of course!

Cheers,
-Mo.
[ April 28, 2005: Message edited by: Moritz Petersen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic