Question: In java, I am reading a file through command prompt (args[0]) and counting the number of strings in that file say n. then I find
In java,
I am reading a file through command prompt (args[0]) and counting the number of strings in that file say n. then I find the first prime number(p) after 2*n. (So if number of strings from file was 5 then fist prime after 2(5) = 11.) With that prime number I construct two arrays of size p: array A and array B
For each string in the file(there will be only one string per line, and all letters are the lowercase a-z), I calculate a "hugeNumber". if the string were the word "ace" it would calculate to 97*26**2 + 99*26**1 + 101*26**0 which would equal the value 68247.
ASCII int associated with each letter For example, 'a' ==97.
'a(97)'*26**2 + 'c'*26**1 + 'e'*26**0 26**2 = 26 squared
with the hugeNumber variablle (ie 68247) I use the hash function to assign it an array index.
arrayIndex= hugeNumber % arraysize(p)
If a collision occurs(two strings hash to same array index) for array A i use linear probing (search next cell to put string into x+1, x+2...).
In array B i use quadratic probing to handle collision (search next cell x+1**2, x+2**2, x+3**2....)
I then Print the contents of arrayA (index, string, and probe length for each insertion, all three on a single line) followed by the contents of B (index, string, and probe length) . Only printing non-empty cells and printing the cells with the smallest index used first.
--This is where i need help-- (probing occurs after a collision occurs) I want to keep track of the probe length for each string inserted into the array and display it whenI am displaing the other. information (you will see when you run it). If the cell is empty when trying to insert then probe length should equal 0. for that specific string. If a collision occurs then I want to keep track of the number of linear or quadratic probes it takes to find an empty cell depending on which array. If I am probing and reach the end of the array, I want to be able to wrap around the array and continue steping through the array as if I had never reached the end (which cell i wrap to would be dependent on which type of probing i am using linerar or quadratic).
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HashApp {
public static void main(String[] args) {
String file1 = args[0];
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file1));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
List fileStrings = new ArrayList<>(); // List to store strings
// in file
String text = "";
try {
while ((text = in.readLine()) != null) {
fileStrings.add(text);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Number of strings in file: " + fileStrings.size());
int p = gefFirstPrimeNumber(fileStrings.size() * 2); // getting firtst prime after 2n
System.out.println("Next Prime number after 2*fileStrings.size(): " + p); System.out.println(""); // if p is not found, it means there are many strings in file which is
// out of integer range, so we exit..
if (p == -1) {
System.out.println("Too many Strings in file..");
System.exit(0);
}
String[] A = new String[p]; // Creating Arrays A and B with size of p.
String[] B = new String[p];
//System.out.println("Calculating hugeNumber for each of the Strings and printing them..");
for (String str : fileStrings) {
long hugeNumber = getHugeNumber(str);
//System.out.println("String: " + str + " hugeNumber " + hugeNumber);
int arrayIndex = (int) (hugeNumber % A.length);
//System.out.println("Array index " + arrayIndex);
int arrayIndexB = arrayIndex; int probeLength = 1;
// case of collision occurs(two strings hash to same array index)
// for array A
// use linear probing (search next cell to put string into x+1,
// x+2...).
while (A[arrayIndex] != null) {
//System.out.println("Detected collision for: " + str);
arrayIndex = arrayIndex + 1;
if (arrayIndex >= p)
break;
}
if (arrayIndex >= p) {
System.out.println("Cannot find a space for this element using linear probing.. not inserting: " + str);
} else {
A[arrayIndex] = str;
}
int i = 1;
// case of collision occurs(two strings hash to same array index)
// for array B
// use quadratic probing (search next cell x+1**2, x+2**2,
// x+3**2....)).
while (B[arrayIndexB] != null) {
arrayIndexB = (int) (arrayIndexB + Math.pow(i, 2));
i++;
if (arrayIndexB >= p)
break;
}
if (arrayIndexB >= p) {
System.out.println(
"Cannot find a space for this element using quadratic probing.. not inserting: " + str);
} else {
B[arrayIndexB] = str;
}
}
System.out.println(" Contents of A: "); for (int i = 0; i < A.length; i++) { if (A[i] != null) { System.out.println("index: " + " string: "+ " probe length:" ); System.out.println( i + "\t " + A[i]); }
}
System.out.println(" Contents of B: ");
for (int i = 0; i < B.length; i++) {
if (B[i] != null) { System.out.println("index: " + " string: "+ " probe length:" ); System.out.println( i + "\t " + B[i]); } }
}
private static long getHugeNumber(String str) {
char[] array = str.toCharArray();
long hugeNumber = 0;
for (int i = 0; i < array.length; i++) {
char c = array[i];
int ascii = c;
if (ascii >= 97 && ascii <= 122) {
hugeNumber += ascii * (Math.pow(26, (array.length - i - 1)));
} else {
System.out.println("not a valid string... exiting");
System.exit(0);
}
}
return hugeNumber;
}
private static int gefFirstPrimeNumber(int n) {
// return next prime number after n
for (int i = n + 1; i < Integer.MAX_VALUE; i++) {
int j = 2;
for (; j < i; j++) {
if (i % j != 0) {
continue;
} else {
break;
}
}
if (j == i) {
return i;
}
}
return -1;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
