• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reader Object  RSS feed

 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have encounter a problem when trying to use a Reader object. i cannot detect the end of file thus the whole word cannot be printed out. Hope someone will enlighten me.






import java.io.*;

public class Tokenizer
{
private Reader reader;

public Tokenizer(Reader r)
{
reader = r;
}

public String nextWord()
{
String word = "";
int next = 0;
char c = 'a';

try
{
while (next != -1)//while "next" not end of input
{
next = reader.read();
if (next != -1) //if not end of input
c = (char) next; //cast "next" into character and store in "c"
word = word + c; //add character to word
}
}
catch (IOException ex)
{
System.out.println("IOException occurs");
}
return word;
}

}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest some braces for this if statement...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!