944,173 Members | Top Members by Rank

Ad:
Oct 26th, 2006
0

array required, but java.lang.String found and java.util.Vector found?

Expand Post »
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 <String>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);
}
}
/*//int hashVal;
if(words.length() < exponent)
{
//for(int i = words.length(); i < exponent; i++ )
while(words.length() < exponent)
{
//words.append("z");
words += "z";
}
}
for(int i = 0; i < exponent; i++)
{
exponent--;
key += (int)(words[i]*(int)(Math.pow((double)base,exponent)));
//key += words[i]*(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<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<;
hash.StrArray[o] = " ";
hash.IntArray[o] = 0;
//System.out.println<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<;
} */
}
//void Hash::quadraticProbing(String &words)
publicvoid quadraticProbing(String words)
{

int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
//hash.readFile(fname,words);
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 = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(hash.IntArray[hashVal] == 1)
{
//System.out.println<<"collission detected "<<hashVal<<" "<<words<<" "<<q<<;
while(hash.IntArray[hashVal] == 1)
{
c++;/**To be removed*/
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
//System.out.println<<(q^2)<<;
//System.out.println<<(int)pow((double)q,2)<<;

if(hashVal >= tableSize)
{/**To be removed*/
//System.out.println<<"\t\tTable size exceeded "<<hashVal<<" "<<words<<" "<<q<< "\n";
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<d<<" 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<<" "<<q<<;
}
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++;

}
}
}
//void Hash::linearProbing2(String &words)
publicvoid linerProbing2(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 = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<;
}
else if(hash.IntArray[hashVal] == 0)
{
//System.out.println<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
elseif(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission2 detected @ "<<hashVal<<" "<<words<<" # "<<r<<;

while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;

if(hashVal >= tableSize)
{/**To be removed*/
// System.out.println<<"\t\tTable size exceeded2 "<<hashVal<<" "<<words<< "\n";
hashVal = hashVal - tableSize;
}
}
//System.out.println<<"\tManaged to hash the word2(" <<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++;
//System.out.println<<"HASH SPACE FOUND2 @ "<<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::quadraticProbing2(String &words)
publicvoid quadraticProbing2(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;
//System.out.println<<"Table "<<table<<" "<<tableSize<<;
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<;
while(hash.IntArray[hashVal] == 1)
{
//c++;
//sum = sum + s.charAt(i)*Math.pow(128,str_len-(i+1));
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
//System.out.println<<" Trying "<<;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<;
}
else if(hash.IntArray[hashVal] == 0)
{
//System.out.println<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
elseif(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<" # "<<r<<;

while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;

if(hashVal >= 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]);
}
}
}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laurentmas is offline Offline
5 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Community Introductions Forum Timeline: Hi
Next Thread in Community Introductions Forum Timeline: Hello from BC Canada





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC