Store word from a file to an array?

Javarama
Javarama used Ask the Experts™
on
how can I store word from a file to an array? and How can I display the contents of the array to a table?

For example;

openFile
       readLine
       stringTokenizer
storeToken in Array ??
closeFile

Generate Table ??
Display
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You'd be better off with java.util.ArrayList as arrays are of fixed size.
Top Expert 2016

Commented:
So:

while (tokenizer.hasMoreTokens()){
  list.add(tokenizer.nextToken());
}
You could also use a Vector that contained String objects.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

For example:

StringTokenizer token = new StringTokenizer("My words");

Vector vec = new Vector(1,1);

while( token.hasMoreTokens() )
{
      vec.add(token.nextToken());
}

Author

Commented:
But how to tokenize a DataInputStream? isnt it not a String?

Author

Commented:
// Convert our input stream to a
// DataInputStream              
DataInputStream in = new DataInputStream(fstream);

// Continue to read lines while
// there are still some left to read
                   
while (in.available() !=0)
    {
     //StringTokenizer tokens = new StringTokenizer ( ? );
     //System.out.println (in.readLine());
    }

But how?
DataInputStream stream = new DataInputStream(InputStream in);

stream.readLine();

However, DataInputStream's readLine finction has been Depricated. It is recommended that you use BufferedReader's readLine() function.

Author

Commented:
// Convert our input stream to a
// DataInputStream              
DataInputStream in = new DataInputStream(fstream);

// Continue to read lines while
// there are still some left to read
                   
while (in.available() !=0)
    {
     //StringTokenizer tokens = new StringTokenizer ( ? );
     //System.out.println (in.readLine());
    }

But how?
Top Expert 2016

Commented:
Why are you using a DataInputStream - what's in the file?

Author

Commented:
I need to read characters such as "<" or ">", that is why im using DataInputStream, what else should i do?

can i use delimiters for that?

Author

Commented:
I need to read characters such as "<" or ">", that is why im using DataInputStream, what else should i do?

can i use delimiters for that?
Top Expert 2016

Commented:
Doesn't sound like a candidate for special treatment to me. Can you give an example of your file format?

Author

Commented:
Ok...let me tell you what I have to do,

1.Open an xml file

2.Read the contents of the file, if it encountered an xml tags, store it.

for example:
<name>
<first>John</first>
<middle>Fitzgerald Johansen</middle>
<last>Doe</last>
</name>

store:

TAGS                          FREQUENCY
<name>                            1
<first>                           1
<middle>                          1
...
....
John                              1
Fitzgerald Johansen               1
....

Top Expert 2016
Commented:
The way to do that sort of thing is to use software specially designed for that purpose. Doing it yourself would be error prone. Look up some examples of SAX parsing. Don't worry if it looks more complex than you'd like. All it is designed for is to read tags and their content accurately, which is exactly what you need.

http://www.plurb.com/misc/xml/SAX.pdf is a good start, although you won't get to what you need until about 3/4 of the way through.

If you're going to be doing future work with Java, or indeed any programming work, you'll need to know this anyway!

Author

Commented:
Thanks again CEHJ....anyway I have develepoed a software that read an xml files and evaluates the output as string in a table....
I have read a lot of xml API in sun website..and i think there a number of useful documentations to have a look...before, i had zero ideas what was SAX and those creepy names was...but the links you gave me helps a lot

anyway...thanks again CEHJ

Author

Commented:
To exceter...thanks for the ideas....it was more than useful
cheers..
You are most welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial