Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  strange hashmap error  (Read 712 times)
0 Members and 2 Guests are viewing this topic.
Offline Kommi

Sr. Member
**

Posts: 293


All opinions will be lined up and shot!


« on: 2006-01-27 16:34:03 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
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(); }
     
     
   }
   






Kommi
Offline Grand Poeba

Full Member
**

Posts: 143



« Reply #1 on: 2006-01-27 19:32:51 »

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);
            }
Offline jbanes

JGO Neuromancer
****

Posts: 1178


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #2 on: 2006-01-28 01:32:38 »

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?

Java Game Console Project
Last Journal Entry: 12/17/04
Games published by our own members! Go get 'em!
Offline Kommi

Sr. Member
**

Posts: 293


All opinions will be lined up and shot!


« Reply #3 on: 2006-01-30 15:02:01 »

ahh thank you. I see the issue now J Banes

Kommi
Offline jbanes

JGO Neuromancer
****

Posts: 1178


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #4 on: 2006-01-30 15:15:55 »

Well since I increment start and end both by 1, anbd the while terminates when end <= data.length(), then I dont see how I can run into that problem

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. Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Offline HamsterofDeath

Jr. Member
**

Posts: 75


Java games rock!


« Reply #5 on: 2006-01-30 15:41:49 »

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

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

btw, don't do
1  
res.append(line + " ");

and complain about " " being in your strings



Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.062 seconds with 19 queries.