• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding an element from arraylist to array  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm trying to write a method which returns the number of vowel characters in arraylist. My idea is to convert the arraylist element by element to array each time iterating through the array counting the vowels of that element.
When I started I immediately got an error(surprise, surprise).
Excuse me if the problem is too simple, but I am very new to programming.

At line 9 I get the following error "Type mismatch: cannot convert from String to int". I want to get the element at this position, not to convert to int....Any suggestions?

 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
No question is too small! We are all learners.

Your error comes from the very end of line 9.
You have the expression
new String[list.get(0)]

This is attempting to create an array of String. How big should that array be? That is determined by the part in square brackets []. In this case you have said that the array should have size list.get(0). The compiler is expecting a number here, to specify the size, but list.get(0) is returning a String.

list was a defined as a parameter to your function to be of type ArrayList<String>. So list.get() will return a String.

I hope that is helpful.


 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to convert a String to an array so that you can iterate over it, you don't need to do that. You can iterate over a String with charAt(). Try that and then show us your code in a new post.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a duplicate of this thread. Werner, don't start a new thread for the same problem.
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway, If anybody ever needs this, I came up with this solution. It's not the best, but it works.
The program finds the average number of vowels in an array of strings.

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java Tutorials and look for Map. There is an example with a Map which counts words. I suggest you change it. Use a Character instead of a String as the “K” in the Map. You can leave the bit about Integer unchanged, or:-

***************************

Create a Counter class for the “V”. Part of it shown below:-If you get to a letter you have used before:-
myMap.get(letter).count();
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go through the Java Tutorials and look for Map. There is an example with a Map which counts words. I suggest you change it. Use a Character instead of a String as the “K” in the Map. You can leave the bit about Integer unchanged, or:-

***************************

Create a Counter class for the “V”. Part of it shown below:-If you get to a letter you have used before:-
myMap.get(letter).count();

Thank you for the suggestion, but the exercise is from the arraylist chapter, which is before what you mentioned. So was "not allowed" to use this in the exercise. Anyway, I will try to make it this way now, thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!