String a != String a


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: String a != String a

  1. #1
    Join Date
    Mar 2005
    Posts
    1

    String a != String a

    The following is a method from a program that compares patterns. The object of this method is to turn an array of strings(characters strings) into an array of numbers like this

    Code:
    Input:    a a b b c
    Output:  0 0 1 1 2
    
    Input:    z c z d a
    Output:  0 1 0 2 3
    I have an array named hash that stores the values of letters already encountered. The problem is when i check the values of the array with the current letter from the string, nothing matches. if you run the program you will see what i am talking about.

    Code:
           public static int[] makeCompareList(String[] characters)
           {
               String[] hash = new String[5]; // Array that stores the number code for each letter
               String checkletter = null;     // Letter currently being checked
               int numrep = 0;                // Number representative for a New letter
               int[] compare = new int[5];    // Number array returned
               for(int characterindex = 0;characterindex < 5;characterindex++) // Loop through inputed strings
               {
                   int newelement = 1;
                   checkletter = characters[characterindex];
                   System.out.println("Character under review: "+checkletter);
                   for(int hashindex = 0;hashindex < 5;hashindex++) // Loop through hash array
                   { 
                       //Error Lies Here
                       if(checkletter.equals(hash[characterindex]))
                        {compare[characterindex] = hashindex; newelement = 0;break;}
                       else {System.out.println(checkletter + " != " + hash[hashindex]);}
                   }
                   if(newelement == 1)
                   {
                       compare[characterindex] = numrep;
                       hash[numrep] = characters[characterindex];
                       System.out.println("Hash["+numrep+"] now equals "+hash[numrep]);
                       numrep ++;
                   }
               }
               return compare;
           }
           
           public static void main(String[] args)
           {
               String[] array = {"a","b","c","d","a"};
               makeCompareList(array);
            }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    And here it is:

    //Error Lies Here
    if(checkletter.equals(hash[characterindex])) // <---- oooopps

    change to;
    if(checkletter.equals(hash[hashindex]))
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center