• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reader Object

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest some braces for this if statement...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic