aspose file tools*
The moose likes Beginning Java and the fly likes Filling an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Filling an ArrayList" Watch "Filling an ArrayList" New topic
Author

Filling an ArrayList

M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
I'm trying to write a program where it takes in a text file and counts all the characters (puncutation and spaces included) and puts each character into a spot in an ArrayList (I don't know the size that's why I figured to use an ArrayList). Anyone know how I go about doing this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Which spot do you want to use? Just the next available one?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Not knowing all the details of your task, I guess a could be a good character frequency counter.

Regards,
Thomas


SCJP 5.0, SCJD in progress
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
Hey, I want to take all the text in the inputed file and be able to count all the characters in it and also have an index number for each character
vikram veera
Ranch Hand

Joined: Dec 26, 2000
Posts: 43
Hi,
You need to use the string tokenizer class to split the string into tokens.
Please find below link which has the code ,which will help solve your problem.In the code it split the string from the file using
string tokenizer class .You need to add those string in Arraylist.

http://www.developerparadise.com/devworld/code/Java/1001JavaIO/JavaIO-1-2-Integerate.php

Thanks
Vikram
www.developerparadise.com
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
I'm not familiar with HashMap, thats why I was thinking of either an Array or ArrayList
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
I thought tokens separeted by whitespace? I need to separate everything and have an index position for everything including whitespace. Can I still use tokens?
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
I'm not familiar with HashMap, thats why I was thinking of either an Array or ArrayList


Still, I think HasMap will be your best friend for what you're planning to do. Have a look at the Javadoc and come back here if you have trouble in understanding.

Regards,
Thomas
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
I thought tokens separeted by whitespace?

That's the default behaviour. You could change that.
I need to separate everything and have an index position for everything including whitespace. Can I still use tokens?

I guess not.
You can convert a String into an array of characters (have a look at the String class) and then iterate the characters. Another interesting method could be ...

Regards,
Thomas
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
I'm really not sure of how to do this since in reading it in from a text file that was inputed through a parameter. I don't know how I access what is in the file.
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Well, you can use a FileReader wrapped by a BufferedReader to read the file line by line into Strings.
Now, if you want to perform some kind of character frequency computations go for the HashMap. if you only want to be able to retrieve character indexes, you can use String.indexOf() for example.
M Shaw
Greenhorn

Joined: Sep 14, 2008
Posts: 11
Thank you for all the help. I am still having a hard time with this though. Sorry for all the questions. I have looked up the BufferedReader but I still don't quite understand what that does. Say I had this as code:

FileReader fr = new File(inFile);
BufferedReader br = new BufferedReader(f);

would I now use the variable br if I wanted to try out the HashMap?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
If you are in Java 5 or Java 6 you can dispense with the BufferedReader by using a java.util.Scanner. But beware: a Scanner takes a delimiter to separate the input into tokens, again by default whitespace.

Please avoid StringTokenizer, which is described in the API documentation as legacy software.

Suggest: find the Scanner class, pass the File to its constructor, find the hasNext() and nextLine() methods and read the lines repeatedly in a while loop.
If you wish to join the lines back to form a String, it is better to use a StringBuilder object and its append(String) method, then toString when you have finished. You can even insert newline characters with an append() call.
The nextLine() method and the BufferedReader.readLine() method do more-or-less the same thing.

I like Thomas Thevis' suggestion about the char[] array. Also his suggestion about a Map<Character, Integer> is good. If the key is already in there, add 1 to the value integer, and put it back; if the key is not already there, put 1.

Try a search through JavaRanch, because other people have faced similar problems in the past
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Try something like this:

After finshing reading the file, you should close the reader by calling


Now you have to decide what to do with the single line. This decision should depend on how do you intend to use the data afterwards.

If you want to perform character frequency mapping, you can try something
like

and in the while loop:

This is not the most elegant and efficient solution, but it should work (if there are no typos in my code).

Regards,
Thomas

[EDIT]
typos
[/EDIT]
[ September 14, 2008: Message edited by: Thomas Thevis ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filling an ArrayList