All pastes #917860 Raw Edit

Someone

public text v1 · immutable
#917860 ·published 2008-02-25 19:11 UTC
rendered paste body
/**
 *  Uses a hash-coded data storage method to store items
 *  from a file. a linked list is used as the method for
 *  dealing with collisions
 *	Uses try, catch, and finally
 */

import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
import java.util.StringTokenizer;

public class Hashing
{
  Hashtable table;

  final int TABLESIZE = 600;

  public Hashing()
  {
    table = new Hashtable(TABLESIZE);
  }

  public void search()
  {
    int idToFind;
    Item location;
    Scanner scan = new Scanner(System.in);
    System.out.println("Testing search algorithm\n");
    System.out.print("Enter Id value to search for (-1 to quit)");
    idToFind = scan.nextInt();
    while (idToFind >= 0)
    {
    	location = (Item)table.find(new Item(idToFind, 0));
    	if (location == null)
    		System.out.println("Id = " + idToFind + " No such part in stock");
    	else
    		System.out.println("Id = " + idToFind + " Inv = " + location.getInv());
    	System.out.println();
    	System.out.print("Enter Id value to search for (-1 to quit)");
    }
   
  }

  public void stats()
  // will print out required stats for this lab
  {
	  DecimalFormat fmt = new DecimalFormat("0.00");
	  
	  System.out.println();
	  int emptySpots = table.getNumberOfNulls();
	  System.out.println("Number of NULLS + " + emptySpots);
	  double pts = (double)emptySpots * 100.0/table.getCapacity();
	  System.out.print("% of NULL poitners for a table of size " );
	  System.out.println(table.getCapacity() + " is " + fmt.format(pts));
	  
	  double avgListLen = (double)table.getSize() / (table.getCapacity() - emptySpots);
	  System.out.println("Average list length = " + fmt.format(avtListLen));
	  
	  System.out.println("The longest list = " + fmt.format(table.getLongestList()));
	  System.out.println();

  }

  public void loadFile() throws IOException
  {
    String fileName = "file30.txt";
    int id, inv;
    String line="";									// declared out of try block so accessible
    BufferedReader inFile = null;		// in later code
    FileReader fr = null;
    
    try
    {
    	fr = new FileReader(fileName);
    	inFile = new BufferedReader(fr);
    	line = inFile.readLine();
    	System.out.println ("ID \t INVENTORY");
    	System.out.println ("First line read " + line);
    	while (line != null)
    	{
    		StringTokenizer tokenizer = new StringTokenizer(line);
    		while (tokenizer.hasMoreTokens())
    			{
    				id = Integer.parseInt(tokenizer.nextToken());
    				System.out.print(id + "\t");
    				inv = Integer.parseInt(tokenizer.nextToken());
    				System.out.println(inv );
    				table.add(new Item(id, inv));
    			}
    		line = inFile.readLine();
    	}
    		
    	}
   	catch(NumberFormatException exception)
    {
    		System.out.println("Error in input.  Line ignored \n");
    		System.out.println(line);
    }
    catch(FileNotFoundException exception)
    {
    	System.out.println ("The file " + fileName  + " was not found \n");
    }
    catch(IOException exception)
    {
    	System.out.println(exception);
    }
    finally
    {
    	if (line != null)
    	{
    		inFile.close();
    	}
		}
  } 
}  
   /* Easier - no try, catch, finally
   	int howMany = 400;
    for (int k = 1; k <= howMany; k++)
    {
      id = inFile.readInt();
      inv = inFile.readInt();
      table.add(new Item(id, inv));
    }
    */