File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Using String Tokenizer... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using String Tokenizer..." Watch "Using String Tokenizer..." New topic
Author

Using String Tokenizer...

Stephen Ostler
Greenhorn

Joined: Mar 26, 2003
Posts: 2
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

Joined: Dec 06, 2001
Posts: 3061
The two lines you have commented out should do the trick. What happens when you un-comment them, compile and run it?


Java API Documentation
The Java Tutorial
Stephen Ostler
Greenhorn

Joined: Mar 26, 2003
Posts: 2
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using String Tokenizer...