• 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

Using String Tokenizer...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic