• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ArrayList Problem

 
Ranch Hand
Posts: 77
  • 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 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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 80267
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80267
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== 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 ]
 
Campbell Ritchie
Marshal
Posts: 80267
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

== 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.
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell and Roger, thanks for your comments. That helps me understand this stuff better.

Thank you,
Dave
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic