Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  strange hashmap error  (Read 1160 times)
0 Members and 1 Guest are viewing this topic.
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Posted 2006-01-27 21: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

Junior Member





« Reply #1 - Posted 2006-01-28 00: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 Coder


Projects: 1


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


« Reply #2 - Posted 2006-01-28 06: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Reply #3 - Posted 2006-01-30 20:02:01 »

ahh thank you. I see the issue now J Banes

Kommi
Offline jbanes

JGO Coder


Projects: 1


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


« Reply #4 - Posted 2006-01-30 20: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

Junior Member




Java games rock!


« Reply #5 - Posted 2006-01-30 20: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (69 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!