This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passing Vector elements to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Vector elements to an array" Watch "Passing Vector elements to an array" New topic
Author

Passing Vector elements to an array

Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Vector elements to an array:
I was wondering how to pass the elements of a vector to an array,
is there a method?

thanks for any response.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Do you mean create an array consisting of the same elements that the Vector contains? There is a method toArray in Vector.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"derok,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc

(As Keith noted above, Vector does indeed have a toArray method. See the API documentation for Vector.)


"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
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Ok, as you can see i used the method you recomend me, here is the code that i am working on, it compiles good. But when printing the vector elements it display "[Ljava.lang.String;@f5da06", and that is of course, not the elements.

Can you tell what is wrong?
The array that i am passing the elements to is "f",
is it well declared?
thanks..

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

OK, two things:

First, toArray() will fill the array you pass in only if it's the exact right size. Otherwise, it will create a new array of the same type as the one you passed in. Since your array is zero length here, the array that actually has the new contents is returned by the toArray()method, but you're not storing that return value anywhere. May I suggest

f = (String[]) v.toArray(f);

Second, when you pass an array (or any object) to println(), println() calls String.valueOf(), which in turn calls toString() on the object you passed to println(). The toString() method for arrays returns a description of the array class followed by "@" and then the hashCode value of the array --i.e., the printout you're getting is the array, it's just not useful.

Ironically, the most convenient way to display the contents of an array is to convert it to a Collection and then display that; this is ironic because you've just converted a Collection to the array. But in any case, if you need to display the array, just say

System.out.println(java.util.Arrays.asList(f));


[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
First of all, invoking println() does not print the contents of the array. Array objects are not like Collection whose toString() method is overriden.

If you are using JDK 1.5 (Tiger) you can use java.util.Arrays.toString() to obtain the desired effect.

The another problem is that you must know that array size is inmutable. That means that if you create an array of size 0, as you did, then you cannot alter the size of the array at runtime.

As you created your array with f={} you array has zero elements. When you pass it as argument to Vector.toArray() there is no room in the array to store elements.
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
thanks a lot Ernest and Edwin.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Do you mean that the same vowel does not repeat?
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
quoting myself:
"hey i need your help in another thing:

if i enter a word on the vector,
how i can determinate that the word entered has different vowels?

for example if enter: [n, a, m, e] "

i mean, for example:
that when i enter a word like the one above, i want the program to print in the console that the word entered contains different vowels.
[ May 08, 2006: Message edited by: Jack Bolton ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
When you say vowels,do you mean [AEIOU] set of characters, or you mean also another vowels in the Unicode Set?
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
yes, only aeiou.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well you could use a simple function to determine the number of times a character appears in your string



Be extremely careful with accented characters and take case into account. Because � is not the same as e, and a is not same as A.


[ May 08, 2006: Message edited by: Edwin Dalorzo ]
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
ok hold on ,

i'm trying to implement the method you posted; in the program.
here it is:



it gives an error when i call it in the main method,
did i declared everything correctly?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You don't have a countLetter method with that signature.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

it gives an error when i call it in the main method,
did i declared everything correctly?


How you declared the countLetter() method, and how you use the countLetter() method, don't match.

You declared the method to take a string and char. If you want to use it, you need to pass it a string and char.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
like this you mean?



it still gives me error:
')' expected
i am using JCreator

[ May 08, 2006: Message edited by: Jack Bolton ]
[ May 08, 2006: Message edited by: Jack Bolton ]
aymane chetibi
Ranch Hand

Joined: Apr 12, 2006
Posts: 175
no this is not correct either.
you have to pass to it the actual values:

Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
Originally posted by aymane chetibi:
no this is not correct either.
you have to pass to it the actual values:



but are you testing the code?
because the program compiles now, but when its running its does not do what the method should do; it's like the method its not even there.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

but are you testing the code?
because the program compiles now, but when its running its does not do what the method should do; it's like the method its not even there.


Jack,

You do realize that everyone is merely showing you how to use the method. We are expecting you to put it in the correct place, using the correct words, and dealing with the counts returned, right?

The test code where it is, doesn't do anything with the rest of the program, nor has any output. You have to modify the rest of you program to correctly use it.

Henry
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
ok, i am implementing the two methods i had, in just one method.




it's giving me this error.
cannot resolve symbol: indexOf method.
why is this happening?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Jack.

Look. I do not believe that mixing all in one method is a good idea. After all reading input from the console have nothing to do with couting the letters in a Vector.

Anyway, this time it fails because your variable f is not a String, but an array of String.

Hence, you should use f[index].indexOf(...)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

it's giving me this error.
cannot resolve symbol: indexOf method.
why is this happening?


In this case, "f" is an array. An arrays don't have an indexOf() method.

Henry
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Look, Jack

You have all the code. You simply need to organize it by responsabilities, and it will flow.

I will give you a few tips, and I am going to post an implementation down there. Try it yourself before reading my implementation:

1. Create a method that simply reads input and returns the String read.
2. Create a method that loads the vector with words read from input (using previous method) and returns the loaded vector.
3. Create a method that loads a Vector into an Array
4. Create a method that prints the vowels for every word in array of Strings, using our previously posted countLetter method.

5. In the main method, simply:
5.1 Load the vector with words using method developed in 2
5.2 Convert Vector to array with metod develped in 3
5.3 Print the number of vowels in every word using method develped in 4

Now, try this yourself first. Then if you want, read my implementation.

A Sample Implementation

For instance, let's create a method that simply reads input and returns the String read. A method which does not know anything of Vectors or arrays or couting letters.



Now, let's create a method that loads the vector with words and returns the loaded vector. It does not know the purpose of the words. All it does is loading words until the word "stop" or "STOP" appears, in whose case method returns.



Now, let's create a method that prints the vowels for every word in array of Strings, using our previously posted countLetter method.



Now a method that loads a Vector into an array



Well, we have all the ingredients. Let's bake the cake. You can create a main method with 3 lines of code with this. Like this



Best regards,
Edwin Dalorzo

[ May 09, 2006: Message edited by: Edwin Dalorzo ]
[ May 09, 2006: Message edited by: Edwin Dalorzo ]
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
thanks Edwin, the problem is always easier if you split it.
the one thing i don't undestand is:
why if i enter a word that begins with a vowel like "apple", the program never prints it?
prints like this:

Vowels found in: apple
a: 0
e: 1
i: 0
o: 0
u: 0
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Jack!

Look, buddy, there is a bug in my logic in the countLetter algorithm. I could tell you, but I have jumped into the conclusion that it might be more helpful to you if this time you ask this question to your debugger.

So, why do you not debug your application and tell us what the bug is?
[ May 09, 2006: Message edited by: Edwin Dalorzo ]
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
in the while of the countLetter, it should be while(index >= 0)
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Good job, buddy!!! That is the bug.

Now, let's drink a beer
Jack Bolton
Ranch Hand

Joined: May 08, 2006
Posts: 65
lol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Vector elements to an array
 
Similar Threads
how to store the vector values in bean
Vectors vs Arrays
Vectors
Converting an array to a Vector -> Simple question with many doubts
Parsing elements of a Vector