I am trying to make a semi-efficient hash table in java and am ending up in an infinite loop -


i have been staring @ thing quiet time , don't see mistake... extremely grateful if point me in right direction. thanks!

public class learninghash {

public static void main(string[] args)  throws ioexception {     java.io.file file = new java.io.file(args[0]); //read data file     scanner input = new scanner(file);     //++++++++++++++++++++++++++++++++++++++++++++++++++++++     int max = 13; // array size     int j = 0;                          // index hash keys     string[] maindata = new string[max];     // items array     string[] inputdata = new string[max];   // temp array     int in = 0;                              // needed variables      long temp;     int loc;     //++++++++++++++++++++++++++++++++++++++++++++++++++++++          while (input.hasnextline()) {  // read file until none left              inputdata[in] = string.valueof(input.hasnextline());              string[] breaks =  inputdata[in].split(" "); // checks space , splits              if(breaks.length == 2) // if equal 2 store i.e "eco 32" become "eco" , "32" therefore ==2             {                temp = hashing(breaks[0], max); // creating hash key                 loc = (int)temp; // converting long int                  if(maindata[loc] != null)                 {                     while(maindata[loc] != null)                     {                         system.out.println("collision @ [" + loc + "] data item : [" + maindata[loc] + "] ");                         loc++;                         if(loc > max)                         {                             loc = 0;                         }                     }                 }                 maindata[loc] = (breaks[0]+" "+breaks[1]); // recombines i.e "moss space 25" , stores in data location                 system.out.println("data item[" + maindata[loc] + "] stored in index [" + loc + "] of array.");             }             else // i.e "eco' without "32" belongs criteria             {                 temp = hashing(breaks[0], max); // creating hash key                 loc = (int)temp; // converting long int                                while((maindata[loc] != breaks[0]) && (maindata[loc] != null) )                 {                     loc++;                     if(loc > max)                         loc = 0;                 }                 if(maindata[loc] == null)                 {system.out.println("error data item ["+ breaks[0] + "] not found. ");}                 else                 {                     system.out.println(" data item [" + breaks[0]+ "] found @ location ["+ loc+"] .");                 }             }      }     input.close(); } 

i think this:

inputdata[in] = string.valueof(input.hasnextline()); 

should replaced with

inputdata[in] = string.valueof(input.nextline()); 

then read out data , not ask if next line available , read boolean out...also think makes infinite loop ;)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -