All pastes #917837 Raw Edit

Miscellany

public text v1 · immutable
#917837 ·published 2008-02-25 18:49 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 algorith\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 = " + location.getID() + " Inv = " + location.getInv());
  		System.out.println();
  		System.out.print("Enter Id value to search for (-1 to quit) --> ");
  		idToFind = scan.nextInt();
  	}
  }


  public void stats()
  {
  	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 pointers 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(avgListLen));
  	
  	System.out.println("The longest list = " + fmt.format(table.getLongestList()));
  	System.out.println();
  }


  public void loadFile() throws IOException
  {
    String fileName = "H:\\HashTable\\file400.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));
    }
    */