This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using String Tokenizer...

 
Stephen Ostler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey can anyone help me - I am trying to implement String Tokenizer into this program. So far the program reads a txt file and then stores every line into an array (and can print them). However, the txt files Ill be dealing with will be made up of just 2 columns of numbers, and Im trying to split each column up and then hold them in 2 new arrays. At the moment im struggling to get the first token into a new array (address). Any ideas?
public String[] getTextLines(File f)
{

final int MAX = 200;
int count = 0;
String[] getTextLines = new String [MAX];
String[] address = new String [MAX];
BufferedReader inFile;

try
{
FileReader fr = new FileReader (f);
inFile = new BufferedReader(fr);
try
{
String line = inFile.readLine();
while (line != null)
{
getTextLines[count++] = line;
line = inFile.readLine();

//tokenizer = new StringTokenizer(line);
//address[count] = tokenizer.nextToken();
}
}

catch (IOException exception)
{
}

}

catch(FileNotFoundException exception)
{
}

return getTextLines;
}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two lines you have commented out should do the trick. What happens when you un-comment them, compile and run it?
 
Stephen Ostler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try and open a text file but instead of it being displayed in the dos prmpt I get an error Exception in thread "main" java.util.NoSuchElementException at my StringTokenizer, Simulator.getTextLines and Simulator.main. I must have done something quite obviously stupid .
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a quick look at The StringTokenizer Class Documentation and note that nextToken() throws NoSuchElementException if there are no more tokens in this tokenizer's string. Also, note some of those other methods return a boolean value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic