troubleshooting Question

Java array blues My array doesn't initialize

Avatar of the_b1ackfox
the_b1ackfoxFlag for United States of America asked on
Java
25 Comments4 Solutions219 ViewsLast Modified:
This is driving me nuts.  Why won't the following code initialize my array?

import java.util.*;

class HashTable
{
        static int ccount;
       
        static String str;
        //static String str2;
        static int hash_table[] = new int[64];
        static Bucket Buckets[] = new Bucket[64];

public static void main(String [] args)
{
      int j;
        int index = 0;
      int free_hash = 64;

      System.out.println( "64 random strings..." );
      System.out.println();

      for(j = 0; j < 64; j++)
      {
            
                make_string();
                index = hash(str);
            if ( 0 == hash_table[index])
                  free_hash--;
            hash_table[index]++;
            String tmpstr=str;
            
            Buckets[index].Add(tmpstr);
      }
      
      System.out.println();
      System.out.println();
      System.out.println( "Hash Table..." );
      System.out.println();

      for(j = 0; j < 64; j++)
      {
            System.out.print( hash_table[j] + "\t" );
            if(7 == j % 8)
                  System.out.println();
      }
      System.out.println();
        System.out.println( "The number of free locations is " + free_hash );
}

static int hash(String name)
{
      int middle, hash_value;
            
        middle = name.length() / 2;
        hash_value = name.charAt(0) + name.charAt(middle) + name.charAt(name.length() - 1);
        hash_value %= 64;
      System.out.print( "hash = " + hash_value + "\t");
      if (++ccount == 3)
      {
            ccount = 0;
            System.out.println();
      }
      return hash_value;
}
static void make_string()
{
      int i,k;

      Random genran = new Random();

      do
      {
            k = genran.nextInt(6);
        } while(k == 0);
        str = "";
        for(i = 0; i < k; i++)
        {
                str += (char)(65 + genran.nextInt(25));
        }
        System.out.print( str + "\t" );
}

public class DCard{
      DCard NextCard;
      String Text;
      DCard(){
            NextCard=null;
            Text="";
      }      
      DCard(String txt){
            Text=txt;
            NextCard=null;
      }
      public boolean EndofLine(){
            boolean results=false;
            if(NextCard==null){
                  results=true;
            }
            return(results);
      }
      
      public void Print(){
            System.out.println("The card I see is called " + Text);
      }

}

public class Bucket{
      DCard Head;
      DCard Current;
      DCard Temp;
      int Count;
      Bucket(){
            Head=null;
            Temp=null;
            Current=Head;
            Count=0;
      }
      public void Clear(){
            
            Head=null;
            Temp=null;
            Current=Head;
            Count=0;
      }
      public void Add(DCard data){
            Current=Head;
            Temp=null;
            while(!Current.EndofLine()){
                  Current=Current.NextCard;
            }
            if(Current.EndofLine()){
                  Temp=data;
                  Current.NextCard=Temp;
                  Temp.NextCard=null;            
            }
            
      }
      
      public void Add(String data){
            String tmpdata=data;
            DCard tmpdatacrd=new DCard(tmpdata);
            Current=Head;
            Temp=null;
            while(!Current.EndofLine()){
                  Current=Current.NextCard;
            }
            if(Current.EndofLine()){
                  Temp=tmpdatacrd;
                  Current.NextCard=Temp;
                  Temp.NextCard=null;            
            }
            
      }
      
      
}

}

ASKER CERTIFIED SOLUTION
Mick Barry
Java Developer
Join our community to see this answer!
Unlock 4 Answers and 25 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 25 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros