• 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

Filling an ArrayList

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which spot do you want to use? Just the next available one?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not knowing all the details of your task, I guess a could be a good character frequency counter.

Regards,
Thomas
 
M Shaw
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with HashMap, thats why I was thinking of either an Array or ArrayList
 
M Shaw
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic