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(); }
}
