Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to fix: Exception in thread main java.lang.NumberFormatException: For input string: moss at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at HashFunction2.main(HashFunction2.java:52) for my hashing program:

How to fix:

Exception in thread "main" java.lang.NumberFormatException: For input string: "moss "

at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.base/java.lang.Integer.parseInt(Integer.java:652)

at java.base/java.lang.Integer.parseInt(Integer.java:770)

at HashFunction2.main(HashFunction2.java:52)

for my hashing program:

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; public class HashFunction2 { String[] theArray; int arraySize; int itemsInArray = 0; public static void main(String[] args) throws IOException { if (args.length == 0) { System.out.println("file names are missing"); System.exit(0); } BufferedReader reader = new BufferedReader(new FileReader(args[0])); String line; int n = 0; while ((line = reader.readLine()) != null) { n++; } reader.close(); HashFunction2 theFunc = new HashFunction2(n); String[] elementsToAdd2 = new String[31]; reader = new BufferedReader(new FileReader(args[0])); while ((line = reader.readLine()) != null) { elementsToAdd2[n++] = line; } theFunc.hashFunction2(elementsToAdd2, theFunc.theArray); // theFunc.modThirty(); theFunc.increaseArraySize(60); // theFunc.displayTheStack(); theFunc.fillArrayWithNeg1(); theFunc.doubleHashFunc(elementsToAdd2, theFunc.theArray); //theFunc.displayTheStack(); // theFunc.findKeyDblHashed("990"); } // Outputs the matches that would put an item in // index 0 if arraySize was 31 public void modThirty() { for (int i = 1; i < 999; i++) { if (i % 30 == 0) { System.out.println(i); } } } public boolean isPrime(int number) { // Eliminate the need to check versus even numbers if (number % 2 == 0) return false; // Check against all odd numbers for (int i = 3; i * i <= number; i += 2) { if (number % i == 0) return false; } // If we make it here we know it is odd return true; } // Receives a number and returns the next prime // number that follows public int getNextPrime(int minNumberToCheck) { for (int i = minNumberToCheck; true; i++) { if (isPrime(i)) return i; } } // Increase array size to a prime number public void increaseArraySize(int minArraySize) { // Get a prime number bigger than the array // requested int newArraySize = getNextPrime(minArraySize); // Move the array into a bigger array with the // larger prime size moveOldArray(newArraySize); } public void moveOldArray(int newArraySize) { // Create an array that has all of the values of // theArray, but no empty spaces String[] cleanArray = removeEmptySpacesInArray(theArray); // Increase the size for theArray theArray = new String[newArraySize]; arraySize = newArraySize; // Fill theArray with -1 in every space fillArrayWithNeg1(); // Send the values previously in theArray into // the new larger array hashFunction2(cleanArray, theArray); } // Will remove all empty spaces in an array public String[] removeEmptySpacesInArray(String[] arrayToClean) { ArrayList stringList = new ArrayList(); // Cycle through the array and if a space doesn't // contain -1, or isn't empty add it to the ArrayList for (String theString : arrayToClean) if (!theString.equals("-1") && !theString.equals("")) stringList.add(theString); return stringList.toArray(new String[stringList.size()]); } public void doubleHashFunc(String[] stringsForArray, String[] theArray) { for (int n = 0; n < stringsForArray.length; n++) { // Store value in array index String newElementVal = stringsForArray[n]; // Create an index to store the value in by taking // the modulus int arrayIndex = Integer.parseInt(newElementVal) % arraySize; // Get the distance to skip down in the array // after a collision occurs. We are doing this // rather than just going to the next index to // avoid creating clusters int stepDistance = 7 - (Integer.parseInt(newElementVal) % 7); System.out.println("step distance: " + stepDistance); /* * System.out.println("Modulus Index= " + arrayIndex + " for value " * + newElementVal); */ // Cycle through the array until we find an empty space while (theArray[arrayIndex] != "-1") { arrayIndex += stepDistance; // System.out.println("Collision Try " + arrayIndex + // " Instead"); // If we get to the end of the array go back to index 0 arrayIndex %= arraySize; } theArray[arrayIndex] = newElementVal; } } // Returns the value stored in the Double Hashed Hash Table public String findKeyDblHashed(String key) { // Find the keys original hash key int arrayIndexHash = Integer.parseInt(key) % arraySize; // Find the keys original step distance int stepDistance = 5 - (Integer.parseInt(key) % 5); while (theArray[arrayIndexHash] != "-1") { if (theArray[arrayIndexHash] == key) { // Found the key so return it System.out.println(key + " was found in index " + arrayIndexHash); return theArray[arrayIndexHash]; } // Look in the next index arrayIndexHash += stepDistance; // If we get to the end of the array go back to index 0 arrayIndexHash %= arraySize; } // Couldn't locate the key return null; } public void hashFunction2(String[] stringsForArray, String[] theArray) { for (int n = 0; n < stringsForArray.length; n++) { String newElementVal = stringsForArray[n]; // Create an index to store the value in by taking // the modulus int arrayIndex = Integer.parseInt(newElementVal) % arraySize; /* * * System.out.println("Modulus Index= " + arrayIndex + " for value " * + newElementVal); */ // Cycle through the array until we find an empty space while (theArray[arrayIndex] != "-1") { ++arrayIndex; // System.out.println("Collision Try " + arrayIndex + // " Instead"); // If we get to the end of the array go back to index 0 arrayIndex %= arraySize; } theArray[arrayIndex] = newElementVal; } } // Returns the value stored in the Hash Table public String findKey(String key) { // Find the keys original hash key int arrayIndexHash = Integer.parseInt(key) % arraySize; while (theArray[arrayIndexHash] != "-1") { if (theArray[arrayIndexHash] == key) { // Found the key so return it System.out.println(key + " was found in index " + arrayIndexHash); return theArray[arrayIndexHash]; } // Look in the next index ++arrayIndexHash; // If we get to the end of the array go back to index 0 arrayIndexHash %= arraySize; } // Couldn't locate the key return null; } HashFunction2(int size) { arraySize = size; theArray = new String[size]; // Fill Array with -1 for each empty space fillArrayWithNeg1(); } public void fillArrayWithNeg1() { Arrays.fill(theArray, "-1"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago