Vector of array [logic error]
General explaination:
Hi, I want to fill a vector of an array of strings tokenized that I pass in the arguments, then. I must fill an array that contain only once each word that appear in all strings, even if there are more than twice, because next step i must do, is to count the times each word is appearing.
That's why I use an string array inside vector, because I need method "split" and Class Vector hasn't any "splitter".
My problem:
Fail is in the condition if(!v.contains(vFrases.elementAt(i)[j])){ that is always true and it should be true when it contains twice a word
Maybe I missunderstood how to fill the array index in a vector
Maybe can I obtain same result I want, with a simple Vector? instead vector of arrays?
Code java:
import java.util.Vector;
public class Ejercicio {
/** MAIN */
public static void main(String[] args) {
if(args.length<1){
System.err.println("Usage: java Ejercicio <string> <string> ");
return;
}
frecuenciasLexicas(args);
}
/** METHOD */
static void frecuenciasLexicas (String[] s){
Vector<String> v = new Vector<String>();
Vector<String[]> vFrases = new Vector<String[]>();
//I fill the vector of arrays with the strings I pass in the args "tokenized" with "split"
for(int i=0; i<s.length ;i++)
vFrases.addElement(s[i].split("\\s"));
for(int i=0; i<vFrases.size() ;i++){
for(int j=0; j<vFrases.elementAt(i).length ;j++){
/** HERE is the PROBLEM, this condition is always false */
/** The vector "v" should contain only once each word that vector "vFrase" has inside, so if v doesn't contain the string, he add the string, but next time will not, because only must have it once each word*/
if(!v.contains(vFrases.elementAt(i)[j])){
v.addElement(vFrases.elementAt(i)[j]+"|");
}
}
for(int k=0; k<v.size();k++)
System.out.println(v.elementAt(k));
}
}
}
ARG EXAMPLE:
"first string" "second string" "third string"
DESIRED OUTPUT (INSIDE V):
v.elementAt(0) -->first|
v.elementAt(1) -->string|
v.elementAt(2) -->second|
v.elementAt(3) -->third|
WHAT I GET:
v.elementAt(0) --> first|
v.elementAt(1) --> string|
v.elementAt(2) --> second|
v.elementAt(3) --> string|
v.elementAt(4) --> third|
v.elementAt(5) --> string|
Sorry for my english XD,
Thank you.
Re: Vector of array [logic error]
Why not use a Map<String, Integer> such as a HashMap<String, Integer> to help you do the counting? The keys for the map would be your String tokens, and the values would be the number of times that the token is present.
Re: Vector of array [logic error]
I added this line first thing in the main() method to get the input you described:
Code :
args = new String[] {"first string", "second string", "third string"}; //<<<<<<<<<<
and get this error:
ArrayIndexOutOfBoundsException: 2 >= 2
Can you show the contents of the console for when you execute the program?
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Vector of array [logic error]
Quote:
Originally Posted by
curmudgeon
Why not use a Map<String, Integer> such as a HashMap<String, Integer> to help you do the counting? The keys for the map would be your String tokens, and the values would be the number of times that the token is present.
I am java begginer, i don't know what is a map /hasmap i am training with vectors and solving problems with them :o
--- Update ---
Quote:
Originally Posted by
Norm
I added this line first thing in the main() method to get the input you described:
Code :
args = new String[] {"first string", "second string", "third string"}; //<<<<<<<<<<
and get this error:
ArrayIndexOutOfBoundsException: 2 >= 2
Can you show the contents of the console for when you execute the program?
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
sorry i edited first post with right code. i forgot a testcode inside the code. fixed
now works
Re: Vector of array [logic error]
Do some debugging by adding a println statement to print out the contents of v and the elementAt()'s value so you can see what is in v and what the contains() method is looking for.
Re: Vector of array [logic error]
Quote:
Originally Posted by
Norm
Do some debugging by adding a println statement to print out the contents of v and the elementAt()'s value so you can see what is in v and what the contains() method is looking for.
i was many hours debugging, my last step is posting here :P (i deleted the maaaany comments of printlns and fors that i have for you dont go crazy :P)
i even drawed it in a paper many times but dont undrstand why i get that output, maybe i dont use good the method "contains"
Re: Vector of array [logic error]
Did you add a println that printed out the values that I suggested? What was printed?
Add the println code for the values I suggested, run the program and copy the print out and paste it here.
Re: Vector of array [logic error]
do you mean this?
Code java:
for(int k=0; k<v.size();k++)
System.out.println(v.elementAt(k));
the API says :
contains(Object o)
Returns true if this vector contains the specified element.
Isn't a string an object? or what's wrong, don't get it
or it is the fact it contains a string that it make it true?
Re: Vector of array [logic error]
Quote:
the condition if(!v.contains(vFrases.elementAt(i)[j])){ that is always true
Print out the two values next to the if statement that uses the contains() method so you can see what the method sees when it executes.
Print the values of v and vFrases.elementAt(i)[j]
Re: Vector of array [logic error]
Code java:
for(int i=0; i<vFrases.size() ;i++){
for(int j=0; j<vFrases.elementAt(i).length ;j++){
System.out.println("vFrases.elementat("+i+")["+j+"] "+vFrases.elementAt(i)[j]);
System.out.println(!v.contains(vFrases.elementAt(i)[j]));
if(!v.contains(vFrases.elementAt(i)[j]))
v.addElement(vFrases.elementAt(i)[j]+"|");
}
for(int k=0; k<v.size();k++)
System.out.println("v.element("+k+"):\t"+v.elementAt(k));
}
It prints:
vFrases.elementat(0)[0]: First
true
vFrases.elementat(0)[1]: string
--> HERE it catch the word "string" for first time, into "v" vector <--
true
v.element(0): First|
v.element(1): string|
vFrases.elementat(1)[0]: Second
true
vFrases.elementat(1)[1]: string
---->HERE SHOULD RETURN FALSE, because it contains already the word "string"<----
true
v.element(0): First|
v.element(1): string|
v.element(2): Second|
v.element(3): string|
vFrases.elementat(2)[0]: Thrid
true
vFrases.elementat(2)[1]: string
---->HERE SHOULD RETURN FALSE TOO,
true
v.element(0): First|
v.element(1): string|
v.element(2): Second|
v.element(3): string|
v.element(4): Thrid|
v.element(5): string|
Re: Vector of array [logic error]
Quote:
it contains already the word "string"
Where do you see "string" in v?
v.element(1) is close: "string|" but it is different.
contains() would not find it as a match!
Re: Vector of array [logic error]
Quote:
Originally Posted by
Norm
Where do you see "string" in v?
v.element(1) is close: "string|" but it is different.
contains() would not find it as a match!
OH MY GOD !
I would delete all my code ant start by zero using only arrays if you don't tell me this.
thank you very much
thank you for your time :rolleyes:
FIXED:
Code java:
if(!v.contains(vFrases.elementAt(i)[j])+"|") /** same string that I add if true.....*/
v.addElement(vFrases.elementAt(i)[j]+"|");
Re: Vector of array [logic error]
I find using println() statements to print out the values of variables and expressions used in if statements useful.
I assume that the computer is right and the I must have made a mistake when the code does not do as I expected.
What is printed out must be carefully examined to see what the computer sees.
Re: Vector of array [logic error]
I'll try to examine strings more carefully hehe. (I had problem added that I may not understood how Vector Methods works. I only know a few methods at the moment).
Have a good day