5 Replies Latest reply: Apr 19, 2009 11:19 PM by 800308 RSS

    Help storing hashed data from file into an array

    843789
      Hi i am trying to store data from a file that has been put through a hashing function into an array but i am stuck and don't know how to do this? Any help would be appreciated.
         import java.util.*;
         import java.io.*;  
           
          interface MyDictionary {
             public boolean contains(String town, String state);
             public int get(String town, String state);
             public int put(String town, String state, int pop);
         }
          class MyHashOne /*implements MyDictionary*/ {
             class Entry {
               String town;
               String state;
               int pop;
            }
            protected Entry[] hashentries;
            protected Entry[] arrayentries;
                protected int[] primes;
            protected int hashcount;
            protected int arraycount;
            protected int hashtabsize; // set to something > 5,796
            protected int datacount; // set to 5,796
             public MyHashOne() {
                 
                  }
                  
             protected int hash(String t, String s){
                 int x=0;
                 for(int i=0; i<t.length(); i++){
                      x=x+t.charAt(i)*primes;
                     }
                for(int i=0; i<s.length(); i++){
                     x=x+s.charAt(i)*primes[i+t.length()];
                     }
                     return x%hashtabsize;
                
                }
                
      public boolean open(String s) {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter File Name");
      System.out.print(">");
      String fname = input.nextLine();
      Scanner fin=null;
      try{
      fin = new Scanner(new FileReader(fname));
      }//end try
      catch (FileNotFoundException e){
      System.out.println("File Not Found");
      open(null);
      }//end catch
      hashentries = new Entry[8000];
                     arrayentries = new Entry[8000];
      while(fin.hasNextLine()){
      String line = fin.nextLine();
                          if(line.length()==0){
                          line=fin.nextLine();
                          }
      String[] tk = line.split("=");
      if (line.startsWith("State=")) {
      String state = tk[1];
                               System.out.println(state);
      }
      else {
      tk = line.split("\\(pop. ");
      String town = tk[0];
                               String pop = tk[1].substring(0, tk[1].length()-1);
                               System.out.println(town+" "+pop);
      }
                          
                          
      }
      return true;



      }
      public int get(String t, String s) {
                Scanner input = new Scanner(System.in);
                System.out.println("Enter State");
                System.out.print(">");
                s = input.nextLine();
                System.out.println("Enter Town");
                System.out.print(">");
                t = input.nextLine();
                int x = hash(t,s);
                return x;
                }
                
      public int put(String t, String s, int pop) {
                Entry e = new Entry();
                     e.town = t;
                     e.state= s;
                
                     int x = hash(t,s);
                     int c = 0;
                          while (hashentries[(x + c) % hashtabsize] != null) {
                               c++;
                          }
                     hashentries[(x + c) % hashtabsize] = e;
           
                          return x;
                }
                
      /* public boolean contains(String town, String state) {
      return true;
      }
                
      protected boolean ishashcomplete() {
      return true;
      }
                
      public void menu() {
                
                }
                public int getprobes{
                
                }
                
                int probecount=0;
                int lookupcount=10000;
                Random r = new Random();
                for(int i = 1; i <= lookupcount; i++(){
                          x = r.nextInt(size);
                          Entry e = arrayentries[x];
                          int p = getprobes(e.town, e.state);
                          probes += p;
                          }
                          double avgprobes = ((double)probecount)/lookupcount;*/
      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        • 1. Re: Help storing hashed data from file into an array
          3004
          1. Your requirements are incredibly vague. "Store data from a file that has been put through a hashing function into an array" tells us next to nothing.

          2. Your problem statement is even more vague. "I am stuck and don't know how to do this" tells us nothing at all.

          What precisely are you trying to to and what specific problems are you having doing it?
          • 2. Re: Help storing hashed data from file into an array
            843789
            Cleaner code to read from:

            import java.util.*;
            import java.io.*;  
                 
            interface MyDictionary
            {
                   public boolean contains(String town, String state);
                   public int get(String town, String state);
                   public int put(String town, String state, int pop);
            }
            
            class MyHashOne /*implements MyDictionary*/
            {
                 class Entry
                 {
                      String town;
                      String state;
                      int pop;
                 }
                 
                 protected Entry[] hashentries;
                 protected Entry[] arrayentries;
                 protected int[] primes;
                 protected int hashcount;
                 protected int arraycount;
                 protected int hashtabsize; // set to something > 5,796
                 protected int datacount; // set to 5,796
                 
                 public MyHashOne()
                 {
                 }
                 
                 protected int hash( String t, String s )
                 {
                      int x=0;
                      for( int i=0; i<t.length(); i++ )
                      {
                           x=x+t.charAt(i)*primes;
                      }
                      for( int i=0; i<s.length(); i++ )
                      {
                           x=x+s.charAt(i)*primes[i+t.length()];
                      }
                      
                      return x%hashtabsize;
                 }
                 
                 public boolean open( String s )
                 {
                      Scanner input = new Scanner(System.in);
                      System.out.print( "Enter File Name\n>" );
                      String fname = input.nextLine();
                      Scanner fin = null;
                      
                      try
                      {
                           fin = new Scanner( new FileReader(fname) );
                      }//end try
                      catch ( FileNotFoundException e )
                      {
                           System.out.println( "File Not Found" );
                           open( null );
                      }//end catch
                      hashentries = new Entry[8000];
                      arrayentries = new Entry[8000];
                      
                      while ( fin.hasNextLine() )
                      {
                           String line = fin.nextLine();
                           if( line.length() == 0 )
                           {
                                line=fin.nextLine();
                           }
                           
                           String[] tk = line.split( "=" );
            if ( line.startsWith( "State=" ) )
                           {
                                String state = tk[1];
                                System.out.println( state );
                           }
                           else
                           {
                                tk = line.split( "\\(pop. " );
                                String town = tk[0];
                                String pop = tk[1].substring( 0, tk[1].length()-1 );
                                System.out.println( town + " " + pop );
                           }
                      }
                      
                      return true;
                 }
                 
                 public int get( String t, String s )
                 {
                      Scanner input = new Scanner( System.in );
                      System.out.print( "Enter State\n>" );
                      s = input.nextLine();
                      System.out.print( "Enter Town\n>" );
                      t = input.nextLine();
                      
                      int x = hash( t, s );
                      
                      return x;
                 }
                 
                 public int put( String t, String s, int pop )
                 {
                      Entry e = new Entry();
                      e.town = t;
                      e.state= s;
                      
                      int x = hash( t, s );
                      int c = 0;
                      
                      while ( hashentries[(x + c)%hashtabsize] != null )
                      {
                           c++;
                      }
                      
                      hashentries[(x + c)%hashtabsize] = e;
                      
                      return x;
                 }
                 
                 /*
                 public boolean contains( String town, String state )
                 {
                      return true;
                 }
                 
                 protected boolean ishashcomplete()
                 {
                      return true;
                 }
                 
                 public void menu()
                 {
                 }
                 
                 public int getprobes
                 {
                 }
                 
                 int probecount = 0;
                 int lookupcount = 10000;
                 Random r = new Random();
                 
                 for( int i = 1; i <= lookupcount; i++ )
                 {
                      x = r.nextInt( size );
                      Entry e = arrayentries[x];
                      int p = getprobes( e.town, e.state );
                      probes += p;
                 }
                 
                 double avgprobes = ((double)probecount)/lookupcount;
                 */
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: Help storing hashed data from file into an array
              843789
              I have a file that has information on states town and population. I am making a database using a hashing funtion. What i need help with is after my open meathod reads the file line by line and puts it in the correct format, i do not know how to get all of the information into the arrays that i created. Each line is converted into and Entry object which is state town and population. I hope that helps you understand what i am trying to do.
              • 4. Re: Help storing hashed data from file into an array
                843789
                if line.length==0: line = line.nextLine() ?
                What if the next line.length() is zero also?
                • 5. Re: Help storing hashed data from file into an array
                  800308
                  scphan wrote:
                  if line.length==0: line = line.nextLine() ?
                  What if the next line.length() is zero also?
                  Then it won't skip that line.... Doh!

                  while(line.length==0) { read line }

                  I missed that one

                  ------

                  Biff,

                  OK, before we start... Why are you rolling-your-own HashTable?

                  Cheers. Keith.