• 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

adding an element from arraylist to array

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

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


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic