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
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:Input: a a b b c Output: 0 0 1 1 2 Input: z c z d a Output: 0 1 0 2 3
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); }


Reply With Quote


Bookmarks