StringTokenizer insert into Array??

N

Newbie

I'm reading the text from a plain text file with the StringTokenizer with a
double loop
(outer to read each line, inner to read each word on each line).

Confused a little how the StringTokenizer really works.

Could some kind soul assist me here as what I really want to do is to
"insert"
each word into a new array.

Each word being an element of the new array that I can manipulate later.

A small example of code on how to achieve this would be extremely grateful!

Thankyou for your continued support to a newbie.
 
C

Chris Smith

Newbie said:
I'm reading the text from a plain text file with the StringTokenizer with a
double loop
(outer to read each line, inner to read each word on each line).

Confused a little how the StringTokenizer really works.

Could some kind soul assist me here as what I really want to do is to
"insert"
each word into a new array.

Each word being an element of the new array that I can manipulate later.

A small example of code on how to achieve this would be extremely grateful!

Thankyou for your continued support to a newbie.

Can you privide a little more context regarding what you're actually
confused about? StringTokenizer is actually rather simple, and looks
like this:

StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
{
String word = st.nextToken();
... // do what you like with the word.
}

As you said, you'll need to wrap that in another loop to do it for every
line of the file. Since I'm unsure exactly what you're doing with the
words when you get them, I leave that part to you as well.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Chris said:
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
{
String word = st.nextToken();
... // do what you like with the word.
}

(One thing that does sometimes confuse people about StringTokenizer:
when you put this code in a loop, you'll still need to create a *new*
StringTokenizer each time through the loop. StringTokenizer objects are
of the disposable "use once and then throw away" variety, and can't be
reused for a different String in the future.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tony Dahlman

Newbie said:
I'm reading the text from a plain text file with the StringTokenizer with a
double loop
(outer to read each line, inner to read each word on each line).

Confused a little how the StringTokenizer really works.

Could some kind soul assist me here as what I really want to do is to
"insert"
each word into a new array.

Each word being an element of the new array that I can manipulate later.

A small example of code on how to achieve this would be extremely grateful!

Thankyou for your continued support to a newbie.

java.text.BreakIterator is actually the best way to get individual words/
sentences/lines etc., out of a text stream or file. There is example code
in the API description. For almost all purposes, I have found it to be
every bit as good as StreamTokenizer and StringTokenizer.

Regards, Tony Dahlman
 
S

Stefan Siegl

Hi I have an solution to offer, but it is surely not the simplest one.
Just have a look, I hope this will help. It will parse a given String
for all words. Notice that i introduced two inner classes to make the
code more readable.

hth, Stefan

public class Test {
class Line{
private List words = new ArrayList();
public void addWord (String word){
words.add(word);
}
public List getWords(){
return words;
}
public String toString(){
StringBuffer ret = new StringBuffer();
ret.append("the words are:\n");
Iterator wIt = words.iterator();
while (wIt.hasNext()){
ret.append(" "+wIt.next()+" \n");
}
return ret.toString();
}
}

class Text{
List lines = new ArrayList();
public void addLine (Line l){
lines.add(l);
}
public List getLines(){
return lines;
}
public String toString(){
StringBuffer ret = new StringBuffer();
ret.append("the lines are:");
Iterator lIt = lines.iterator();
while (lIt.hasNext()){
ret.append(lIt.next());
}
return ret.toString();
}
}

public Text parseText (String text){
Text toReturn = new Text();

StringTokenizer outer = new StringTokenizer(text, "\n");
while (outer.hasMoreElements()){
String line = (String) outer.nextElement();
//create the inner StringTokenizer and search for the words
StringTokenizer inner = new StringTokenizer(line, " ");
Line currentLine = new Line();
while (inner.hasMoreElements()){
currentLine.addWord((String) inner.nextElement());
}
// add the line object to the text
toReturn.addLine(currentLine);
}
return toReturn;
}

public static void main (String args[]){
String theText = "I am the text from the file \n Please use
java.io to parse me \n thanks";
System.out.println(new Test().parseText(theText));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top