Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Vector elements to an array

 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.)
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot Ernest and Edwin.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that the same vowel does not repeat?
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, only aeiou.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have a countLetter method with that signature.
 
Henry Wong
author
Marshal
Pie
Posts: 20899
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no this is not correct either.
you have to pass to it the actual values:

 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20899
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20899
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the while of the countLetter, it should be while(index >= 0)
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job, buddy!!! That is the bug.

Now, let's drink a beer
 
Jack Bolton
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic