2 Replies - 631 Views - Last Post: 27 March 2015 - 12:50 PM Rate Topic: -----

#1 linux96   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 15

Map not being filled properly

Posted 27 March 2015 - 12:43 PM

My code keeps giving me a nullpointer exception in my generate() method. I ran a debugger and the issue is in my addItems() method. It's not properly assigning values the way it should. It's supposed to be a Markuv chain of prefix to suffix values. the prefixes can be any number of words according to the ORDER variable. The first Prefix value is supposed to be an empty string with the suffix as the first word from the iterator. I watched it add this to the map but then it over writes the prefix value with the first word from the iterator. it also assigns what should be the previous suffix to the current prefix. I think these errors are occurring because i didn't override my equals() and hashCode() method in the nested prefix class. Can someone please tell me why my map is not being filled properly? This is my first post to DIC so if I need to elaborate more on my code or my question let me know.

import java.util.*;

import com.sun.org.apache.xpath.internal.operations.Equals;

public class StringChain {
    private final int ORDER;

    private Map<Prefix, Suffix> MARKUV_MAP = new HashMap<Prefix, Suffix>();

    public StringChain(int order) {
        ORDER = order;
    }

    public void addItems(Iterator<String> itemIter) {
        
// Create Prefix to be the currentPrefix, assign it as a non-word prefix
        // Create String to represent the currentSuffix assign it the first word
        Prefix currentPrefix = new Prefix();
        String currentSuffix = itemIter.next();

        // Put the first prefix key and suffix in the map
        MARKUV_MAP.put(currentPrefix, new Suffix(currentSuffix));

        /*Assign currentPrefix to represent the next prefix with the current
         *suffix added on to the end*/
        currentPrefix = new Prefix(currentPrefix, currentSuffix);
        
        // loop through the iterator
        while (itemIter.hasNext()) {
            
            // Assign currentSuffix to the next word
            currentSuffix = itemIter.next();

            // test if the currentPrefix key already exists
            if (!(MARKUV_MAP.containsKey(currentPrefix))) {
               
                // put the key and value into the map
                MARKUV_MAP.put(currentPrefix, new Suffix(currentSuffix));
            
            } else {
                
/* Create a temporary Suffix from the suffix assigned to the key
                 * add the currentSuffix to the tempSuffix */
                Suffix tempSuffix = MARKUV_MAP.get(currentPrefix);
                tempSuffix.add(currentSuffix);

                // assign the new value tempSuffix to the existing key
                MARKUV_MAP.put(currentPrefix, tempSuffix);
            }
            // assign currentPrefix to the next prefix
            currentPrefix = new Prefix(currentPrefix, currentSuffix);
        }

    }

    public List<String> generate(int n, Random rand) {
        List<String> gibberish = new LinkedList<String>();

        Prefix current = new Prefix();

        for (int i = 0; i < n; i++) {
            gibberish.addAll(current.get());

            Suffix suffixProbabilities = MARKUV_MAP.get(current);
            String suffix = suffixProbabilities
                    .getIndex(rand
                            .nextInt(suffixProbabilities.SuffixProbabilities
                                    .size() - 1));
            gibberish.add(suffix);
            current = new Prefix(current, suffix);
        }

        return gibberish;
    }

    /**
     * prefix class represents an arrayList of string from the text file. There
     * will be multiple strings depending on the ORDER constant.
     * 
     * @author zach
     */
    private class Prefix {
        // array list to hold the strings
        private final List<String> PREFIX = new LinkedList<>();

        Prefix(Prefix pre, String word) {
            // get the ArrayList from pre and assign it to a temporary array
            LinkedList<String> tempList = pre.get();

            // tempArray.remove(0); //remove the first element
            // tempArray.trimToSize();//trim the array to its size
            tempList.removeFirst();
            tempList.add(word);// add the new word to the array
            PREFIX.addAll(tempList);// add the array to this Prefix class array
        }

        Prefix() {
            // construct a non-word prefix based off the ORDER constant
            for (int i = 0; i < ORDER; i++) {
                PREFIX.add("");
            }
        }

        public LinkedList<String> get() {
            return (LinkedList<String>) PREFIX;
        }

        public boolean equals(Object obj) {
            if (PREFIX == obj)
                return true;
            try {
                Prefix other = (Prefix) obj;

                for (int i = 0; i < PREFIX.size(); i++) {
                    if (!(this.PREFIX.get(i).equals(other.get().get(i)))) {
                        return false;
                    }
                }
                return true;
            } catch (ClassCastException io) {
                return false;
            }
        }

        @Override
        public int hashCode() {
            // int hash = 0;
            //
            // for (int i = 0; i < PREFIX.size(); i++) {
            // hash += PREFIX.get(i).hashCode();
            // }
            //
            // return hash;
            return PREFIX.hashCode();
        }

    }

    private class Suffix {
        // Map<String,Double> suffixProbabilities = new HashMap<>();
        List<String> SuffixProbabilities = new LinkedList<>();

        Suffix(String word) {
            SuffixProbabilities.add(word);
        }

        public void add(String newWord) {
            SuffixProbabilities.add(newWord);
        }

        public String getIndex(int i) {
            return SuffixProbabilities.get(i);
        }

        @Override
        public int hashCode() {
            return SuffixProbabilities.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            try {
                Suffix other = (Suffix) obj;

            } catch (ClassCastException io) {
                return false;
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Map not being filled properly

#2 linux96   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 15

Re: Map not being filled properly

Posted 27 March 2015 - 12:48 PM

My code keeps giving me a nullpointer exception in my generate() method. I ran a debugger and the issue is in my addItems() method. It's not properly assigning values the way it should. It's supposed to be a Markuv chain of prefix to suffix values. the prefixes can be any number of words according to the ORDER variable. The first Prefix value is supposed to be an empty string with the suffix as the first word from the iterator. I watched it add this to the map but then it over writes the prefix value with the first word from the iterator. it also assigns what should be the previous suffix to the current prefix. I think these errors are occurring because i didn't override my equals() and hashCode() method in the nested prefix class. Can someone please tell me why my map is not being filled properly? This is my first post to DIC so if I need to elaborate more on my code or my question let me know.

import java.util.*;

import com.sun.org.apache.xpath.internal.operations.Equals;

public class StringChain {
    private final int ORDER;

    private Map<Prefix, Suffix> MARKUV_MAP = new HashMap<Prefix, Suffix>();

    public StringChain(int order) {
        ORDER = order;
    }

    public void addItems(Iterator<String> itemIter) {
        
         // Create Prefix to be the currentPrefix, assign it as a non-word prefix
        // Create String to represent the currentSuffix assign it the first word
        Prefix currentPrefix = new Prefix();
        String currentSuffix = itemIter.next();

        // Put the first prefix key and suffix in the map
        MARKUV_MAP.put(currentPrefix, new Suffix(currentSuffix));

        /*Assign currentPrefix to represent the next prefix with the current
         *suffix added on to the end*/
        currentPrefix = new Prefix(currentPrefix, currentSuffix);
        
        // loop through the iterator
        while (itemIter.hasNext()) {
            
            // Assign currentSuffix to the next word
            currentSuffix = itemIter.next();

            // test if the currentPrefix key already exists
            if (!(MARKUV_MAP.containsKey(currentPrefix))) {
               
                // put the key and value into the map
                MARKUV_MAP.put(currentPrefix, new Suffix(currentSuffix));
            
            } else {
                
                 /* Create a temporary Suffix from the suffix assigned to the key
                 * add the currentSuffix to the tempSuffix */
                Suffix tempSuffix = MARKUV_MAP.get(currentPrefix);
                tempSuffix.add(currentSuffix);

                // assign the new value tempSuffix to the existing key
                MARKUV_MAP.put(currentPrefix, tempSuffix);
            }
            
            // assign currentPrefix to the next prefix
            currentPrefix = new Prefix(currentPrefix, currentSuffix);
        }
    }

    public List<String> generate(int n, Random rand) {
        List<String> gibberish = new LinkedList<String>();

        Prefix current = new Prefix();

        for (int i = 0; i < n; i++) {
            gibberish.addAll(current.get());

            Suffix suffixProbabilities = MARKUV_MAP.get(current);
            String suffix = suffixProbabilities
                    .getIndex(rand
                            .nextInt(suffixProbabilities.SuffixProbabilities
                                    .size() - 1));
            gibberish.add(suffix);
            current = new Prefix(current, suffix);
        }

        return gibberish;
    }

    /**
     * prefix class represents an LinkedList of string from the text file. There
     * will be multiple strings depending on the ORDER constant.
     * 
     * @author ****
     */
    private class Prefix {
        // array list to hold the strings
        private final List<String> PREFIX = new LinkedList<>();

        Prefix(Prefix pre, String word) {
            // get the LinkedList from pre and assign it to a temporary array
            LinkedList<String> tempList = pre.get();

            tempList.removeFirst();   //remove the first element
            tempList.add(word);       // add the new word to the array
            PREFIX.addAll(tempList);  // add the array to this Prefix class array
        }

        Prefix() {
            // construct a non-word prefix based off the ORDER constant
            for (int i = 0; i < ORDER; i++) {
                PREFIX.add("");
            }
        }

        public LinkedList<String> get() {
            return (LinkedList<String>) PREFIX;
        }

        public boolean equals(Object obj) {
            if (PREFIX == obj)
                return true;
            try {
                Prefix other = (Prefix) obj;

                for (int i = 0; i < PREFIX.size(); i++) {
                    if (!(this.PREFIX.get(i).equals(other.get().get(i)))) {
                        return false;
                    }
                }
                return true;
            } catch (ClassCastException io) {
                return false;
            }
        }

        @Override
        public int hashCode() {
            return PREFIX.hashCode();
        }

    }

    private class Suffix {
        List<String> SuffixProbabilities = new LinkedList<>();

        Suffix(String word) {
            SuffixProbabilities.add(word);
        }

        public void add(String newWord) {
            SuffixProbabilities.add(newWord);
        }

        public String getIndex(int i) {
            return SuffixProbabilities.get(i);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15780
  • View blog
  • Posts: 63,237
  • Joined: 12-June 08

Re: Map not being filled properly

Posted 27 March 2015 - 12:50 PM

Please do not create duplicate topics. Closing.

edit:
merge posts in response to PM.

This post has been edited by modi123_1: 27 March 2015 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1