Using a Map; Not Storing Correctly?
  This will take a little explanation, and is a very complex problem all around.  Alright - looking at the first bit of code, PropTextWithMap, I am currently attempting to traverse a large text file (a book actually, Alice in Wonderland), and store a specified string length nGram from beginning to end into a Map (MyMap) as the key, and the character that follows as the value the key points to.  It appears to work correctly - I run with it for a little while in the Debugger, and it works fine.

  But then I run into a little issue when attempting to access the value of one of the keys.  Logic dictates that eventually there will be some overlap - that one of these keys will end up holding multiple values (which is why the value is an ArrayList that is updated when a new value is added).  The problem is, when I attempt to use the picknGramAndPrint, it is able to find the nGram the first time within the map, but it will always print 'D' for some odd reason (it is supposed to print a random value found in the relative ArrayList).  And then, it is unable to find the next nGram (the previous minus the first letter plus the letter chosen from the ArrayList).  I cannot for the life of my figure out why.

Essentially, either the bloody thing isn't storing the values in the ArrayList correctly - which it seems too - or it isn't traversing the map correctly.  I've included all classes below; It's one heck of a problem, and I'm not sure if anyone can help, but thank you if you try.

PropTextWithMap.java

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;

    public class PropTextWithMap {
	public static void main(String[] args) {
		String file_name;
		int nGramLength;
		int numberOfCharacters;

		Scanner keyboard = new Scanner(System.in);
		MyMap<String, ArrayList<Character>> m = new MyMap<String, ArrayList<Character>>();

		System.out.print("Source File Name: ");
		file_name = keyboard.nextLine();
		System.out.print("nGramLength: ");
		nGramLength = keyboard.nextInt();
		System.out.print("Number of Characters: ");
		numberOfCharacters = keyboard.nextInt();

		StringBuilder Stored = readFilenGramIntoStringBuilder(file_name);

		m = createMapOfnGrams(Stored, nGramLength, m);

		picknGramAndPrint(m, nGramLength, numberOfCharacters, Stored);
	}

	private static void picknGramAndPrint(
			MyMap<String, ArrayList<Character>> m, int nGramLength,
			int numberOfCharacters, StringBuilder stored) {
		Random randomGenerator = new Random();
		int subBegin = 0;
		char steal;
		String currentnGram;
		ArrayList<Character> nGramFollowers = new ArrayList<Character>();

		subBegin = randomGenerator.nextInt(stored.length() - nGramLength);
		currentnGram = stored.substring(subBegin, subBegin + nGramLength);
		nGramFollowers = m.get(currentnGram);
		steal = nGramFollowers.get(randomGenerator.nextInt(nGramFollowers
				.size()));
		System.out.print(steal);

		for (int i = 1; i < numberOfCharacters; i++) {
			currentnGram = currentnGram.substring(1)
					+ steal;
			nGramFollowers = m.get(currentnGram);
			
			steal = nGramFollowers.get(randomGenerator.nextInt(nGramFollowers
					.size()));
			System.out.print(steal);
			if (steal == ' ' && (-5 <= (i % 100) - 60 || (i % 100) - 60 <= 10))
				System.out.print("\n");

		}
	}

	private static MyMap<String, ArrayList<Character>> createMapOfnGrams(
			StringBuilder stored, int nGramLength,
			MyMap<String, ArrayList<Character>> m) {

		ArrayList<Character> passer = new ArrayList<Character>();

		for (int i = 0; i < stored.length() - nGramLength; i++) {
			passer.clear();
			if (m.get(stored.substring(i, i + nGramLength)) != null)
				passer = m.get(stored.substring(i, i + nGramLength));
			passer.add(stored.charAt(i + nGramLength));
			m.put(stored.substring(i, i + nGramLength), passer);
		}

		return m;
	}

	private static Scanner my_inputFile;

	private static StringBuilder readFilenGramIntoStringBuilder(String file_name) {
		StringBuilder returner = new StringBuilder();

		try {
			my_inputFile = new Scanner((new File(file_name)));
		} catch (FileNotFoundException e) {

		}

		while (my_inputFile.hasNext()) {
			returner.append(my_inputFile.nextLine());
		}

		return returner;

	}
    }

    public class MyMap<K extends Comparable<K>, V> {
	private class MapNode {
		private K key;
		private V value;
		private MapNode left;
		private MapNode right;

		public MapNode(K theKey, V theValue) {
			key = theKey;
			value = theValue;
			left = null;
			right = null;
		}
	}

	private MapNode root;

	public MyMap() {
		root = null;
	}

	/**
	 * Associates key to value and stores mapping If key exists, replaces value
	 * with a new value
	 * 
	 * @param key
	 * @param value
	 * @return value replaced; null if no value
	 */

	public V put(K key, V value) {
		return put(key, value, root);
	}

	private V put(K key, V value, MapNode ref) {
		if(root == null){
			root = new MapNode(key, value);
			return null;
		} else {
			if (ref.key.compareTo(key) == 0) {
				V temp = ref.value;
				ref.value = value;
				return temp;
			} else if (key.compareTo(ref.key) < 0){
				if(ref.left!=null)
					return put(key, value, ref.left);
				else{
					ref.left = new MapNode(key, value);
					return null;
				}
			} else{
				if(ref.right!=null)
					return put(key, value, ref.right);
				else{
					ref.right = new MapNode(key, value);
					return null;
				}
			}
		}
	}

	/**
	 * Return value to which key is mapped
	 * 
	 * @param key
	 * @return value of key; null
	 */

	public V get(K key) {
		return get(key, root);
	}

	private V get(K key, MapNode ref) {
		if (ref == null) {
			return null;
		} else {
			if (ref.key.compareTo(key) == 0)
				return ref.value;
			else if (key.compareTo(ref.key) < 0)
				return get(key, ref.left);
			else
				return get(key, ref.right);
		}
	}

	/**
	 * Returns true if Map already uses the key
	 * 
	 * @param key
	 * @return true; false
	 */

	public boolean containsKey(K key) {
		return containsKey(key, root);
	}

	private boolean containsKey(K key, MapNode ref) {
		if (ref == null) {
			return false;
		} else {
			if (ref.key.compareTo(key) == 0)
				return true;
			else if (key.compareTo(ref.key) < 0)
				return containsKey(key, ref.left);
			else
				return containsKey(key, ref.right);
		}
	}
    }

MyMap.Java

    public class MyMap<K extends Comparable<K>, V> {
	private class MapNode {
		private K key;
		private V value;
		private MapNode left;
		private MapNode right;

		public MapNode(K theKey, V theValue) {
			key = theKey;
			value = theValue;
			left = null;
			right = null;
		}
	}

	private MapNode root;

	public MyMap() {
		root = null;
	}

	/**
	 * Associates key to value and stores mapping If key exists, replaces value
	 * with a new value
	 * 
	 * @param key
	 * @param value
	 * @return value replaced; null if no value
	 */

	public V put(K key, V value) {
		return put(key, value, root);
	}

	private V put(K key, V value, MapNode ref) {
		if(root == null){
			root = new MapNode(key, value);
			return null;
		} else {
			if (ref.key.compareTo(key) == 0) {
				V temp = ref.value;
				ref.value = value;
				return temp;
			} else if (key.compareTo(ref.key) < 0){
				if(ref.left!=null)
					return put(key, value, ref.left);
				else{
					ref.left = new MapNode(key, value);
					return null;
				}
			} else{
				if(ref.right!=null)
					return put(key, value, ref.right);
				else{
					ref.right = new MapNode(key, value);
					return null;
				}
			}
		}
	}

	/**
	 * Return value to which key is mapped
	 * 
	 * @param key
	 * @return value of key; null
	 */

	public V get(K key) {
		return get(key, root);
	}

	private V get(K key, MapNode ref) {
		if (ref == null) {
			return null;
		} else {
			if (ref.key.compareTo(key) == 0)
				return ref.value;
			else if (key.compareTo(ref.key) < 0)
				return get(key, ref.left);
			else
				return get(key, ref.right);
		}
	}

	/**
	 * Returns true if Map already uses the key
	 * 
	 * @param key
	 * @return true; false
	 */

	public boolean containsKey(K key) {
		return containsKey(key, root);
	}

	private boolean containsKey(K key, MapNode ref) {
		if (ref == null) {
			return false;
		} else {
			if (ref.key.compareTo(key) == 0)
				return true;
			else if (key.compareTo(ref.key) < 0)
				return containsKey(key, ref.left);
			else
				return containsKey(key, ref.right);
		}
	}
    }



Using a Map; Not Storing Correctly?
===

  This will take a little explanation, and is a very complex problem all around.  Alright - looking at the first bit of code, PropTextWithMap, I am currently attempting to traverse a large text file (a book actually, Alice in Wonderland), and store a specified string length nGram from beginning to end into a Map (MyMap) as the key, and the character that follows as the value the key points to.  It appears to work correctly - I run with it for a little while in the Debugger, and it works fine.

  But then I run into a little issue when attempting to access the value of one of the keys.  Logic dictates that eventually there will be some overlap - that one of these keys will end up holding multiple values (which is why the value is an ArrayList that is updated when a new value is added).  The problem is, when I attempt to use the picknGramAndPrint, it is able to find the nGram the first time within the map, but it will always print 'D' for some odd reason (it is supposed to print a random value found in the relative ArrayList).  And then, it is unable to find the next nGram (the previous minus the first letter plus the letter chosen from the ArrayList).  I cannot for the life of my figure out why.

Essentially, either the bloody thing isn't storing the values in the ArrayList correctly - which it seems too - or it isn't traversing the map correctly.  I've included all classes below; It's one heck of a problem, and I'm not sure if anyone can help, but thank you if you try.

PropTextWithMap.java

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;

    public class PropTextWithMap {
	public static void main(String[] args) {
		String file_name;
		int nGramLength;
		int numberOfCharacters;

		Scanner keyboard = new Scanner(System.in);
		MyMap<String, ArrayList<Character>> m = new MyMap<String, ArrayList<Character>>();

		System.out.print("Source File Name: ");
		file_name = keyboard.nextLine();
		System.out.print("nGramLength: ");
		nGramLength = keyboard.nextInt();
		System.out.print("Number of Characters: ");
		numberOfCharacters = keyboard.nextInt();

		StringBuilder Stored = readFilenGramIntoStringBuilder(file_name);

		m = createMapOfnGrams(Stored, nGramLength, m);

		picknGramAndPrint(m, nGramLength, numberOfCharacters, Stored);
	}

	private static void picknGramAndPrint(
			MyMap<String, ArrayList<Character>> m, int nGramLength,
			int numberOfCharacters, StringBuilder stored) {
		Random randomGenerator = new Random();
		int subBegin = 0;
		char steal;
		String currentnGram;
		ArrayList<Character> nGramFollowers = new ArrayList<Character>();

		subBegin = randomGenerator.nextInt(stored.length() - nGramLength);
		currentnGram = stored.substring(subBegin, subBegin + nGramLength);
		nGramFollowers = m.get(currentnGram);
		steal = nGramFollowers.get(randomGenerator.nextInt(nGramFollowers
				.size()));
		System.out.print(steal);

		for (int i = 1; i < numberOfCharacters; i++) {
			currentnGram = currentnGram.substring(1)
					+ steal;
			nGramFollowers = m.get(currentnGram);
			
			steal = nGramFollowers.get(randomGenerator.nextInt(nGramFollowers
					.size()));
			System.out.print(steal);
			if (steal == ' ' && (-5 <= (i % 100) - 60 || (i % 100) - 60 <= 10))
				System.out.print("\n");

		}
	}

	private static MyMap<String, ArrayList<Character>> createMapOfnGrams(
			StringBuilder stored, int nGramLength,
			MyMap<String, ArrayList<Character>> m) {

		ArrayList<Character> passer = new ArrayList<Character>();

		for (int i = 0; i < stored.length() - nGramLength; i++) {
			passer.clear();
			if (m.get(stored.substring(i, i + nGramLength)) != null)
				passer = m.get(stored.substring(i, i + nGramLength));
			passer.add(stored.charAt(i + nGramLength));
			m.put(stored.substring(i, i + nGramLength), passer);
		}

		return m;
	}

	private static Scanner my_inputFile;

	private static StringBuilder readFilenGramIntoStringBuilder(String file_name) {
		StringBuilder returner = new StringBuilder();

		try {
			my_inputFile = new Scanner((new File(file_name)));
		} catch (FileNotFoundException e) {

		}

		while (my_inputFile.hasNext()) {
			returner.append(my_inputFile.nextLine());
		}

		return returner;

	}
    }

    public class MyMap<K extends Comparable<K>, V> {
	private class MapNode {
		private K key;
		private V value;
		private MapNode left;
		private MapNode right;

		public MapNode(K theKey, V theValue) {
			key = theKey;
			value = theValue;
			left = null;
			right = null;
		}
	}

	private MapNode root;

	public MyMap() {
		root = null;
	}

	/**
	 * Associates key to value and stores mapping If key exists, replaces value
	 * with a new value
	 * 
	 * @param key
	 * @param value
	 * @return value replaced; null if no value
	 */

	public V put(K key, V value) {
		return put(key, value, root);
	}

	private V put(K key, V value, MapNode ref) {
		if(root == null){
			root = new MapNode(key, value);
			return null;
		} else {
			if (ref.key.compareTo(key) == 0) {
				V temp = ref.value;
				ref.value = value;
				return temp;
			} else if (key.compareTo(ref.key) < 0){
				if(ref.left!=null)
					return put(key, value, ref.left);
				else{
					ref.left = new MapNode(key, value);
					return null;
				}
			} else{
				if(ref.right!=null)
					return put(key, value, ref.right);
				else{
					ref.right = new MapNode(key, value);
					return null;
				}
			}
		}
	}

	/**
	 * Return value to which key is mapped
	 * 
	 * @param key
	 * @return value of key; null
	 */

	public V get(K key) {
		return get(key, root);
	}

	private V get(K key, MapNode ref) {
		if (ref == null) {
			return null;
		} else {
			if (ref.key.compareTo(key) == 0)
				return ref.value;
			else if (key.compareTo(ref.key) < 0)
				return get(key, ref.left);
			else
				return get(key, ref.right);
		}
	}

	/**
	 * Returns true if Map already uses the key
	 * 
	 * @param key
	 * @return true; false
	 */

	public boolean containsKey(K key) {
		return containsKey(key, root);
	}

	private boolean containsKey(K key, MapNode ref) {
		if (ref == null) {
			return false;
		} else {
			if (ref.key.compareTo(key) == 0)
				return true;
			else if (key.compareTo(ref.key) < 0)
				return containsKey(key, ref.left);
			else
				return containsKey(key, ref.right);
		}
	}
    }

MyMap.Java

    public class MyMap<K extends Comparable<K>, V> {
	private class MapNode {
		private K key;
		private V value;
		private MapNode left;
		private MapNode right;

		public MapNode(K theKey, V theValue) {
			key = theKey;
			value = theValue;
			left = null;
			right = null;
		}
	}

	private MapNode root;

	public MyMap() {
		root = null;
	}

	/**
	 * Associates key to value and stores mapping If key exists, replaces value
	 * with a new value
	 * 
	 * @param key
	 * @param value
	 * @return value replaced; null if no value
	 */

	public V put(K key, V value) {
		return put(key, value, root);
	}

	private V put(K key, V value, MapNode ref) {
		if(root == null){
			root = new MapNode(key, value);
			return null;
		} else {
			if (ref.key.compareTo(key) == 0) {
				V temp = ref.value;
				ref.value = value;
				return temp;
			} else if (key.compareTo(ref.key) < 0){
				if(ref.left!=null)
					return put(key, value, ref.left);
				else{
					ref.left = new MapNode(key, value);
					return null;
				}
			} else{
				if(ref.right!=null)
					return put(key, value, ref.right);
				else{
					ref.right = new MapNode(key, value);
					return null;
				}
			}
		}
	}

	/**
	 * Return value to which key is mapped
	 * 
	 * @param key
	 * @return value of key; null
	 */

	public V get(K key) {
		return get(key, root);
	}

	private V get(K key, MapNode ref) {
		if (ref == null) {
			return null;
		} else {
			if (ref.key.compareTo(key) == 0)
				return ref.value;
			else if (key.compareTo(ref.key) < 0)
				return get(key, ref.left);
			else
				return get(key, ref.right);
		}
	}

	/**
	 * Returns true if Map already uses the key
	 * 
	 * @param key
	 * @return true; false
	 */

	public boolean containsKey(K key) {
		return containsKey(key, root);
	}

	private boolean containsKey(K key, MapNode ref) {
		if (ref == null) {
			return false;
		} else {
			if (ref.key.compareTo(key) == 0)
				return true;
			else if (key.compareTo(ref.key) < 0)
				return containsKey(key, ref.left);
			else
				return containsKey(key, ref.right);
		}
	}
    }


