Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Vector of array [logic error]

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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?

     
    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.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vector of array [logic error]

    I added this line first thing in the main() method to get the input you described:
          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.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vector of array [logic error]

    Quote Originally Posted by curmudgeon View Post
    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

    --- Update ---

    Quote Originally Posted by Norm View Post
    I added this line first thing in the main() method to get the input you described:
          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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vector of array [logic error]

    Quote Originally Posted by Norm View Post
    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 (i deleted the maaaany comments of printlns and fors that i have for you dont go crazy )

    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"

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vector of array [logic error]

    do you mean this?
    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?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vector of array [logic error]

    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]
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vector of array [logic error]

    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|

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vector of array [logic error]

    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!
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (February 3rd, 2013)

  13. #12
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vector of array [logic error]

    Quote Originally Posted by Norm View Post
    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

    FIXED:
    if(!v.contains(vFrases.elementAt(i)[j])+"|")    /**  same string that I add if true.....*/
    	v.addElement(vFrases.elementAt(i)[j]+"|");

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (February 3rd, 2013)

  16. #14
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Not sure what the Logic Error is? [help]
    By Mitsuwa in forum Object Oriented Programming
    Replies: 2
    Last Post: January 27th, 2013, 11:55 PM
  2. Can you help me find my logic error
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2012, 01:51 AM
  3. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  4. convert vector to array of bytes
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: May 3rd, 2010, 11:00 AM
  5. Convert Vector to Byte Array
    By perlWhite in forum Java Theory & Questions
    Replies: 0
    Last Post: August 25th, 2009, 05:45 AM