aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Problem" Watch "ArrayList Problem" New topic
Author

ArrayList Problem

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
I'm trying to write a simple program that reads a file into an ArrayList, passes the list into a method that counts the number of vowels in each element of the list, and then returns the average vowel count of the entire list.

I don't know why I'm getting these two errors when I try to compile it:

VowelCount.java:25: illegal start of expression
public static double averageVowels(ArrayList<String> list){
^
VowelCount.java:39: ';' expected
}
^
2 errors

Here is the code for the entire program:
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, I figured that problem out myself. I didn't have the main method closed. So fixed that. Now it's telling me that I can't fill an ArrayList the same way I can an array, like String[] s = {"this","is","an","array","of","words"};

So now my question is, can you fill an ArrayList in some similar way?

Thanks,
Dave
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Yes and no. If what you need is a List<String> that doesn't necessarily have to be an ArrayList you can use:


If it has to be an ArrayLits you can use:

[ March 17, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, thanks Garrett. Now I'm stuck on the core method of this program. I need to count up all the vowels in my ArrayList of Strings. So my psuedocode is something like: count each vowel in each String in my ArrayList. Here's what I have which doesn't complile:

It gives me these errors:
VowelCount.java:37: cannot find symbol
symbol : variable length
location: class java.lang.String
for(int i = 0; i < s.length; i++){
^
VowelCount.java:38: i is already defined in averageVowels(java.util.ArrayList<ja
va.lang.String>
for(int i = 0; i <= vowels.length; i++){
^
VowelCount.java:39: cannot find symbol
symbol : method charAt(int)
location: class VowelCount
if(charAt(i).equals(vowels[i])){
^
3 errors
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, here's my code along with the errors I'm getting now:


VowelCount.java:37: cannot find symbol
symbol : method length()
location: class char[]
for(int i = 0; i <= vowels.length(); i++){
^
VowelCount.java:39: char cannot be dereferenced
if(s.charAt(j).equals(vowels[i])){
^
2 errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dave DiRito:
...VowelCount.java:37: cannot find symbol
symbol : method length()
location: class char[]
for(int i = 0; i <= vowels.length(); i++){...

In an array, "length" is a field -- not a method.
Originally posted by Dave DiRito:
...VowelCount.java:39: char cannot be dereferenced
if(s.charAt(j).equals(vowels[i])){...

s.charAt(j) returns a primitive char, and you can't call a method (like equals) on a char.
[ March 17, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Thanks Marc. Changed my code as follows and got rid of the first error, but still don't get the second. Don't know what to use there. What I want it to do is compare the character in the word from the ArrayList, one at a time, to each vowel from the vowel array. When there's a match, count it. I think that's how I need to accomplish it anyway.

VowelCount.java:39: char cannot be dereferenced
if(s.charAt(j).compareTo(vowels[i])){
^
1 error
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Just changed it to this which compiles but still has a problem when I run it cuz it doesn't find any matches:


Still working on it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

put in a ton of print statements and see what your code is doing at each step. you may want to also use a rather simple input file for a while as well...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
vowels is an array of Characters,
charAt() returns a char

== will compare if two things are the same thing,

if you want to use the ==, explicitly get the char out of the Character


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Yes, thanks, Fred. That's just what I was doing and I found that for some reason an empty ArrayList is getting passed to my vowel counting method even though there is something in the ArrayList. Here's the whole code and then the output:

[this, file, has, seven, words, in, it.] 7
0

0.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Have you declared vowel count as an int? You can't get fractional values from int arithmetic; if you divide a / b and a is less than b, you don't get a fraction, you get 0. Try 1.0 * a / b instead.

And you are passing the List of Strings to the method which calculates the number of vowels, but not using it. You are using the other List declared locally; this is empty.

Lose your declaration of list and use strings throughout in that method. You will probably find an improvement.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Also check your inner for loop; you are using int j followed by i++. These little spelling errors cause no end of annoyance because computers are naive; they believe what you tell them implicitly!
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
YES! Seems to be working correctly now. A big thank you to everyone who gave suggestions. I did catch that erroneous i, Campbell. Didn't think anyone else had noticed that one. You guys are good!

Just a couple questions. What about what Bill Shirley's == comment? I was of the same understanding that == is true only it's the exact same thing, but it seems to work in this case.

Campbell said to use strings throughout. Campbell, are you saying that I should change the char vowell array into a string array of single character strings?

Thanks again, guys. You are great! Don't know how I'd learn this stuff without your help.

Dave
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
== only works on actual primitives, for everything else it is likely best to try .equals( ... ); method of whatever you are trying to test. Place print statements all over the place or in some way look and see what the code is actually doing. Dream up edge conditions, write checks for them as well as write iteration tests that work all possible values you can think of.

for: char vowell array into a string array of single character strings

try something like this until you get a better grasp of the char v Character.


[ March 17, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Originally posted by Dave DiRito:
Campbell said to use strings throughout. Campbell, are you saying that I should change the char vowell array into a string array of single character strings?

Dave
Well done.

Case-sensitivity. There is strings which is the name of the parameter in your method to calculate the average. There are also Strings which are objects of the String class. What I means was to get rid of what you called list and use what you called strings instead.

No, don't change the char[] array.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
== only works on actual primitives, for everything else it is likely best to try .equals( ... ); method of whatever you are trying to test.

The equality operator returns true if both variables reference the same object. In my view, it is much preferable to use == whenever you can. For one thing, it adds clarity to the code you are writing. And testing will tell you if you are using the operator correctly.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Campbell and Roger, thanks for your comments. That helps me understand this stuff better.

Thank you,
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Problem