This article has been dead for over three months
You
Hi there,
I try to implement a Hash class but i get these errors:
- array required, but java.lang.String found &
- array required, but java.util.Vector found.
here is my code:
import java.io.*;
import java.text.*;
import java.lang.*;
import java.util.*;
import java.util.Vector;
import essential.*;
//import keyboard.*;
publicclass HashCode
{
//public vector fileVec; //vector that stores words from the file
// Create an instance of class Vector ...
Vector fileVec = new Vector();
//int *IntArray; //array that stores hash keys
publicint [] IntArray;
//String *StrArray; //array that stores hashed words
public String [] StrArray;
//String words; //words read from a file
HashCode hash = new HashCode();
String fname, words;
int tableSize = 0;
//Creating a BufferedReader object
static BufferedReader bf =
new BufferedReader(new InputStreamReader(System.in));
// Create a number formatter object
static NumberFormat aNumberFormatter = NumberFormat.getInstance();
int key;
char z;
int tSize = 1009;
char ch;
int n,base;
long exponent;
//int exponent;
//int base;
int c = 0; int g5 = 0;
int q = 0; int p = 1;
int x = 0; int g6 = 0;
int g0 = 0; int g7 = 0;
int g1 = 0; int g8 = 0;
int g2 = 0; int g9 = 0;
int g3 = 0; int g10 = 0;
int g4 = 0;
int y = 0;
//public void Hash();
//public int hashFunction(int key)
//{
// return key % arraySize;
//}
//int Hash::getHashFunction()
publicint getHashFunction()
{
//Scanner keyboard = new Scanner(System.in);
char ch;
//enter:
//while(cin.get(ch))
//while( bf.read(ch))
//nextValue = System.in.read();
//ch = (char) System.in.read();
//while(ch)
//do
//{
//if((ch >= '0') && (ch <= '9'))
while((ch >= '0') && (ch <= '9'))
{
//cin.putback(ch);
//ch = (char) bf.read();
ch = (char) System.in.read();
n = aNumberFormatter.parse(bf.readLine()).intValue();
break;
}
//else
//goto enter;
//exit do;
//}
return n;
}
//int Hash::Key(String &words,long int &exponent,int &base)
publicint key(String words, long exponent, int base)
{
//String word [] = new String[2001];
int hashVal;
//if(words.length() < exponent)
if(words.length() < exponent)
{
//for(int j = 0; j < (exponent- words.length()); j++)
while(words.length() < exponent)
{
words += "z";
System.out.println(words+" "+"appended\n");
}
for(int i = 0; i < exponent; i++)
{
//key += (words[i]*(int)(pow((double)base,(exponent-(i+1)))));
key += words[i]*(base+exponent);
}
}
else
{
for(int t = 0; t < exponent; t++)
{
//key += (double)(words[t]*(int)(pow((double)base,(double)exponent)));
key += words[t]*(base<>exponent);
}*/
return hashVal = (int)(key%tableSize);
}
//bool isPrime(int n)
publicboolean isPrime(int n)
{
if(n < 2)
return false;
for(int i = 2; i*i <= n; i++)
{
if(n%2 == 0)
return false;
}
return true;
}
//int nextPrime(int n)
publicint nextPrime(int n)
{
if(n%2 == 0)
n++;
for(; !isPrime(n); n+=2)
;
return n;
}
int r = 0; /**to be removed*/
//void Hash::linearProbing(String &words)
publicvoid linearProbing(String words)
{
int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
System.out.println("Enter any one of the following hash functions: \n");
System.out.println(" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n");
System.out.println();
System.out.println("Hash Function: ");
//exponent = hash.getHashFunction();
base = hash.getHashFunction();
System.out.println("Enter table size: ");
tableSize = aNumberFormatter.parse(bf.readLine()).intValue();
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new String [tableSize];
hash.IntArray = new int [tableSize];
//int table = (0.1*tableSize);
int table;
table %= tableSize;
for( int i = 0; i < tableSize; i++)
{
words = hash.fileVec[y];
hashVal = hash.key(words,exponent,base);
y++;
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
System.out.println("collission detected @ "+hashVal+" "+words+" # "+r);
while(hash.IntArray[hashVal] == 1)
{
c++; //counter for collissions
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
System.out.println("\t\tTable size exceeded111 "+ hashVal+ " " +words + "\n");
hashVal = hashVal%tableSize;
}
}
hash.IntArray[hashVal] = 1; /**To be removed*/
hash.StrArray[hashVal] = words;
System.out.println("\tManaged to hash the word(" +hash.StrArray[hashVal]+
")@ "+ hashVal+" after "+c+" clashe(s) ");
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0; /**To be removed*/
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("HASH SPACE FOUND @ "+hashVal+" "+words+" # "+r);
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
r++; /**To be removed*/
}
/*
for(int o = 0; o < tableSize; o++)
{
//System.out.println<<" "<= tableSize)
{/**To be removed*/
//System.out.println<<"\t\tTable size exceeded "<= tableSize)
{
hashVal = hashVal - tableSize;
}
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" < table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission2 detected @ "<= tableSize)
{/**To be removed*/
// System.out.println<<"\t\tTable size exceeded2 "<= tableSize)
{
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" < table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission detected @ "<= tableSize)
{/**To be removed*/
System.out.println("\t\tTable size exceeded "+hashVal+" "+words+ "\n");
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
}
}
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("\tManaged to hash the word(" +hash.StrArray[hashVal]+ ")@ "+hashVal+" after "+c+" clashe(s) ");
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0;
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("HASH SPACE FOUND @ "+hashVal+" "+words+" # "+r);
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table))
|| (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table))
|| (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill ");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
r++; /**To be removed*/
}
}
}
//void Hash::makeEmpty(int &tableSize)
publicvoid makeEmpty(int tableSize)
{
for(int i = 0; i < tableSize; i++)
{
hash.IntArray[i] = 0;
hash.StrArray[i] = " ";
}
}
//void Hash::drawMenu()
publicvoid drawMenu()
{
System.out.println();
System.out.println("\t\t ___________________MENU__________________\n");
System.out.println("\t\t| |\n");
System.out.println("\t\t| Specify option(1 or 2) |\n");
System.out.println("\t\t| 1.Real Insertion |\n");
System.out.println("\t\t| 2.Virtual Insertion |\n");
System.out.println("\t\t| a. Linear Probing |\n");
System.out.println("\t\t| i. Hash Function |\n");
System.out.println("\t\t| ii.Table Size |\n");
System.out.println("\t\t| b. Quadratic Probing |\n");
System.out.println("\t\t| i. Hash Function |\n");
System.out.println("\t\t| ii.Table Size |\n");
System.out.println("\t\t| 3. Exit |\n");
System.out.println("\t\t|_________________________________________|\n");
}
publicvoid readFile(String fname,String words,int tableSize)
throws Exception
{
//Initialising and declaring the array to read words into
//String temp [] = new String[2001];
String StrArray [] = new String[2001];
//Creating a file object
File word = new File("data.txt");
if(word.exists()&& word.canRead())
{
//Creating a buffered reader object and wrapping in it a Filereader object
BufferedReader reader = new BufferedReader(new FileReader(word));
//Reading in the words from the text file into the array
for(int n=0; n<2001; n++)
{
StrArray [n] = reader.readLine();
}
reader.close();
//To print the array with words from the text file
for(int i=0; i<2001; i++ )
{
System.out.println("Word at "+"["+i+"]"+" is " +StrArray[i]);
}
}
}
}