samedi 28 novembre 2015

Determining if certain letters are in HashMap value of Strings

So I have written pretty much all of the homework code and reached the ending part. I have gone through the debugger and as far as I can see it should work and have traced the issue to the exact spot of problem and would like some help in determining why this is happening and a possible way around it.

public class ReadFile {

public static void main(String[] args) throws IOException {

    /** Hardwire table from homework to use for code */
    String file_name =
                "C:/Users/Shane/Documents/College/Classes/PurchaseTable.txt";

    extract(file_name, 50);

}

private String path;

public ReadFile(String file_path) {
    path= file_path;
}

public String[] OpenFile() throws IOException {
    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = readLines();
    String[] textData = new String[numberOfLines];

    int i;

    for(i=0; i < numberOfLines; i++) {
        textData[i] = textReader.readLine();
    }

    textReader.close();
    return textData;
}

int readLines() throws IOException {

    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while(( aLine = bf.readLine()) != null) {
        numberOfLines++;
    }
    bf.close();

    return numberOfLines;
}

public static void extract(String filename, int threshold) {

    String file_name = filename;
    ArrayList<String> temp = new ArrayList<String>();
    ArrayList<String> products = new ArrayList<String>();
    HashMap<Integer, String> productsPerDate = new HashMap<Integer, String>();
    ArrayList<String> combos = new ArrayList<String>();
    HashMap<String, Integer> allCombinations = new HashMap<String, Integer>();

    try {
        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();
        int i;
        //excludes header section of any table as shown in assignment
        for (i=1; i < aryLines.length; i++) { 
            temp.add(aryLines[i]);              
        }
    }
    catch (IOException e) {
        System.out.println( e.getMessage() );
    }

    System.out.println(temp);
    System.out.println(temp.get(0));
    System.out.println(temp.size());

    int i; int j; int l;
    for (i=0; i<temp.size(); i++) {
        String str = temp.get(i);
        StringBuilder sb = new StringBuilder(str);
        int k =0;
        for (j=0; j<=sb.length(); j++) {
            if(sb.charAt(j) == '\"' && k==0) {
                sb.delete(0, j+1);
                k++;
            }
            if(sb.charAt(j) == '\"' && k!=0) {
                sb.delete(j, sb.length());
                String line = null;
                System.out.println(sb);
                for( l=0; l<sb.length(); l++) {
                     String string = Character.toString(sb.charAt(l));
                     if(string.equals(",")) {

                     }
                     else if (l ==0) {
                         products.add(string);
                         line = string;
                     }
                     else {
                         products.add(string);
                         line = line + string;
                     }
                }
                productsPerDate.put(i, line);
                //System.out.println(products);
                break;
            }
        }
    }

    System.out.println(products);
    //Hashmap set to string of 1 letter characters for products per date
    System.out.println(productsPerDate.entrySet()); 

    Set<String> removeDup = new HashSet<>();
    removeDup.addAll(products);
    products.clear();
    products.addAll(removeDup);

    System.out.println(products);


    int maxLength = productsPerDate.get(0).length();
    //determine max length of string in hashmap
    for(int m = 0; m < productsPerDate.size(); m++) { 
        if(maxLength < productsPerDate.get(m).length()) {
            maxLength = productsPerDate.get(m).length();
        }
    }

    //String allLetters //= products.get(0);
    String allLetters = null;
    for(int m=0; m < products.size(); m++) {
        if(m==0) {
            allLetters = products.get(m);
        }
        else {
            allLetters = allLetters + products.get(m);
        }
    }

    //accounts for combination method where each time it will return combinations of 
    for(int n=1; n<=maxLength; n++) { 
        combos = combinations(allLetters, n); //products of length n
        System.out.println(combos);
        int comboSize = combos.size();
        for(int p=0; p<comboSize; p++) {
            StringBuilder sbCombos = new StringBuilder(combos.get(p));


            for(int q=0; q<productsPerDate.size(); q++) {
                StringBuilder sbCompareto = new StringBuilder(productsPerDate.get(q));  //compareto productsPerDate

                threshold(sbCombos, sbCompareto, allCombinations);
            }               
        }

    }   
}

public static ArrayList<String> combinations(String nChars, int k) {
    ArrayList<String> combos = new ArrayList<String>();
    int n = nChars.length();
    if (k == 0) {
        combos.add("");
        return combos;
    }
    if (n < k || n == 0)
        return combos;
    String last = nChars.substring(n-1);
    combos.addAll(combinations(nChars.substring(0, n-1), k));
    for (String subCombo : combinations(nChars.substring(0, n-1), k-1)) 
        combos.add(subCombo + last);

    return combos;
}

public static void threshold(StringBuilder stringBuilder1, StringBuilder stringBuilder2, HashMap<String, Integer> finalHashMap) {
    boolean exist = false;
    for(int q=0; q<stringBuilder1.length(); q++) {
        if(stringBuilder2.indexOf(Character.toString(stringBuilder1.charAt(q))) > -1) {     
            exist = true;
        }
        else {
            exist = false;
            break;
        }
    }
    if(exist = true) {
        String combo = null;
        for(int r=0; r<stringBuilder1.length(); r++) {
            if(r==0) {
                combo = Character.toString(stringBuilder1.charAt(r));
            }
            else {
                combo = combo + Character.toString(stringBuilder1.charAt(r));
            }
        }
        boolean contains = finalHashMap.containsKey(combo);
        if( contains = false) {
            finalHashMap.put(combo, 25);
        }
        else {
            finalHashMap.put(combo, finalHashMap.get(combo)+25);
        }
    }
}

}

Sorry that the code is quite lengthy the assignment wasn't done in the most efficient fashion but please bear with it. The problem is traced to the code segments below:

public static void threshold(StringBuilder stringBuilder1, StringBuilder stringBuilder2, HashMap<String, Integer> finalHashMap) {
    boolean exist = false;
    for(int q=0; q<stringBuilder1.length(); q++) {
        if(stringBuilder2.indexOf(Character.toString(stringBuilder1.charAt(q))) > -1) {     
            exist = true;
        }
        else {
            exist = false;
            break;
        }
    }
    if(exist = true) {
        String combo = null;
        for(int r=0; r<stringBuilder1.length(); r++) {
            if(r==0) {
                combo = Character.toString(stringBuilder1.charAt(r));
            }
            else {
                combo = combo + Character.toString(stringBuilder1.charAt(r));
            }
        }
        boolean contains = finalHashMap.containsKey(combo);
        if( contains = false) {
            finalHashMap.put(combo, 25);
        }
        else {
            finalHashMap.put(combo, finalHashMap.get(combo) + 25);
        }
    }
}

Most specifically at the

boolean contains = finalHashMap.containsKey(combo);
if( contains = false) {
        finalHashMap.put(combo, 25);
}

where the contains inside the if statement is not using the contains declared above so it never first initializing a value of 25 in the finalHashMap and thus every value is null at the end of the code. Can anyone please help? It would be greatly appreciated for the assignment and my personal learning! Thank you!

Aucun commentaire:

Enregistrer un commentaire