strange hashmap error

I created a little app that steps through a text file with binary values, counting the how many different sequences appear. The hashmap is set up so that the key is a binary string (ex: 110011) and the value is an int that gets incremented every time this sequence is found in the file. The weird error I am getting is that after I am done with my search, I got ot print out the results, but some of the binary strings have holes in them coming out of the hashmap. An example output looks like this:

key = :1000010000:
key = :1110011000:
key = :1010100000:
key = :0000001011:
key = :001001111 :
key = :1110011100:
key = :0000010111:
key = :1011110101:
key = :0000110111:

so some of the strings have missing 1’s or 0’s

My code can be found here: www.kommi.com/example.rar the input is the binary.txt and the output is the count.txt (everything is set up, just run the java file)

But there is nothing special about it here it is:

Map<String, Integer> map = new HashMap<String, Integer>();
	
	String data;
	
	testCounter(String file)
	{
		data = loadFile(file);
	}
	
	private void runSequenceCount(int length, String output)
	{
		int start = 0;
		int end = length;
		Integer found;
		String sequence = "";
		
		
		
		while (end <= data.length() )
		{
			sequence = data.substring(start, end);
			
			found = map.get(sequence);
	        
	        
	        
	            if (found != null) 
	            {
	                map.put(sequence,found + 1);
	                
	                
	            } 
	            else 
	            {
	                map.put(sequence,1);
	            }
	            
	        
	        end++;
	        start++;
		
		}
	
		printMap(map, output);
		
				
	}
	
	public void printMap(Map aMap, String output)
	{
		int mapsize = aMap.size();
		
		StringBuffer res = new StringBuffer();
		Iterator keyValuePairs1 = aMap.entrySet().iterator();
		
		try
		{
			PrintWriter out = new PrintWriter(new FileWriter(output) );
			//set the iterator to point at length - limit
			for (int i = 0; i < mapsize; i++)
			{
				  
				  Map.Entry entry = (Map.Entry) keyValuePairs1.next();
				  Object key = entry.getKey();
				  Object value = entry.getValue();
				  
				  System.out.println("key = :" + entry.getKey() + ":");
				  //res.append(key + " , " + value + "\r\n");
				  
				  out.println("key = :" + entry.getKey() + ":");
				  
			}
		
		}
		catch (IOException e){ e.printStackTrace(); } 
		
		
	}
	

try replacing
found = map.get(sequence);

        if (found != null)
        {
            map.put(sequence,found + 1);
        }
        else
        {
            map.put(sequence,1);
        }

with
if (map.containsKey(sequence))
{
map.put(sequence,found + 1);
}
else
{
map.put(sequence,1);
}

  1. Your version would give an incorrect count, since found is never pulled from the map.
  2. That’s assuming that your version would compile. Which it won’t. (“found” is never defined)

If I were to take a wild ass guess out of the blue, I’d come up with a single question: Kommi, how do you guarantee that you will always have length characters to read from the screen? i.e. What happens if you want to read 10 characters, but only have 9 left?

ahh thank you. I see the issue now J Banes

[quote=“Kommi,post:4,topic:26015”]
Add a System.out.println(start+":"+end+":"sequence); right after the sequence = data.substring(start, end); line to verify that this is actually holding true. There might be some odd logic bug that just isn’t obvious. :slight_smile:

that would just slow it down, unless there is a null-sequence.

btw, don’t do


res.append(line + " ");

and complain about " " being in your strings