Results 1 to 6 of 6
  1. #1
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Searching a string in a vector

    I have a vector say vector v = [(1), (2), (1 2)]
    Now if I try to find the index of a string s={"1"} in the vector v, I get the answer, ie, 0 in this case.
    But I cannot find the index of a string such as string s={"1 2"}. I get -1 as answer although I shud be getting 2 in this case. The complete program is as shown below . Please suggest me a solution. Thankyou.

    Java Code:
    import java.io.*;
    import java.util.*;
     
    class test
    {  
     public static void main(String args[])
     {
     Vector v1=new Vector();
     v1.addElement("1");
     v1.addElement("2");
     v1.addElement("3");
     v1.addElement("1 2");
     v1.addElement("1 3");
     v1.addElement("2 3");
      
     String itemset="1 2 3";
     String[] ss=itemset.split(" ");
     int len=ss.length;
     
        int i,j;
        String str1;
        String str2=new String();
        String str3=new String();
     
        if (len==1);
        else{  
        for (i=1;i<=len;i++) {
          StringTokenizer st=new StringTokenizer(itemset);
          str1=new String();
          for (j=1;j<i;j++) {
            str1=str1.concat(st.nextToken());
            str1=str1.concat(" ");
          }
          str2=st.nextToken();
          for (j=i+1;j<=len;j++) {
            str1=str1.concat(st.nextToken());
            str1=str1.concat(" ");
          }
          
          int ind1=v1.indexOf(str1);
          int ind2=v1.indexOf(str2);
     
          System.out.println(str1);
          System.out.println(ind1);
          System.out.println(str2);
          System.out.println(ind2);
          
        }//end for
        }//end else
       
     }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I could be off base since I an unable to compile your present code, but have you tried to trim str1 before trying to use it to find an indexOf?

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    str1=str1.concat(st.nextToken());
    str1=str1.concat(" ");
    ...is always going to append a trailing " " onto your strings longer than 1, so there won't be a match.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toadaly View Post
    ...is always going to append a trailing " " onto your strings longer than 1, so there won't be a match.
    Yup, that's what I suspected. And while my suggestion to trim this trailing space will work, what you're implying -- to fix the String at its creation -- is probably a better solution.

  5. #5
    coolFrenzi is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    Trimming the string worked. Thanks toadaly and Fubarable.

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Please go to the top of the page and click Thread Tools -> Mark This Thread As Solved.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 03:31 AM
  2. How i add string array in vector
    By susan in forum New To Java
    Replies: 7
    Last Post: 02-12-2009, 02:32 PM
  3. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 10:12 PM
  4. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 10:50 AM
  5. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •