File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes while loop with an if loop (help) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop with an if loop (help)" Watch "while loop with an if loop (help)" New topic
Author

while loop with an if loop (help)

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
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

Joined: Apr 14, 2005
Posts: 13
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.


Tonia
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40



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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 19, 2004
Posts: 67
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Moritz Petersen
Greenhorn

Joined: Apr 28, 2005
Posts: 4
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop with an if loop (help)