How to fix this exception? Please help! the small file is a bunch of string like
ID: 3847138 • Letter: H
Question
How to fix this exception? Please help!
the small file is a bunch of string like below, it is too much, so i cannot copy all of them.
import java.util.Scanner;
import java.util.Vector;
import java.util.Arrays;
import java.io.File;
import java.lang.Math;
public class HashTable{
//The size of the hash table.
int TableSize;
//The current number of elements in the hash table.
int NumElements;
//The variable T represents the array used for the table.
// DECLARE YOUR TABLE DATA STRUCTURE HERE
String[] T;
public HashTable(){
NumElements = 0;
TableSize = 997;
T=new String[TableSize];
// INITIALZE YOUR TABLE DATA STRUCTURE HERE
}
/* hash(s) = ((3^0)*s[0] + (3^1)*s[1] + (3^2)*s[2] + ... + (3^(k-1))*s[k-1]) mod TableSize
(where s is a string, k is the length of s and s[i] is the i^th character of s)
Return the hash code for the provided string.
The returned value is in the range [0,TableSize-1].
NOTE: do NOT use Java's built in pow function to compute powers of 3. To avoid integer
overflows, write a method that iteratively computes powers and uses modular arithmetic
at each step.
*/
public int hash(String s){
int h = 0;
for(int i=0; i h += s.charAt(i)*pow(i);
}
return h%TableSize;
}
public int pow(int p){
int ans=3;
if(p==0){
ans=1;
return ans;
}
if(p==1){
return ans;
}
for(int i=2;i<=p;i++){
ans*=ans;
}
return ans;
}
/* insert(s)
Insert the value s into the hash table and return the index at
which it was stored.
*/
public int insert(String s){
NumElements++;
if((double)NumElements/TableSize>=0.75){
TableSize=TableSize*2;
resize(TableSize);
}
int i = hash(s);
int j = 0;
int k = i;
while(T[k]!= null){
j++;
k=(i+(j*j))%TableSize;
}
T[k]=s;
return k;
}
/* find(s)
Search for the string s in the hash table. If s is found, return
the index at which it was found. If s is not found, return -1.
*/
public int find(String s){
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element))
return k;
j++;
k=(i+(j*j))%TableSize;
}
}
/* remove(s)
Remove the value s from the hash table if it is present. If s was removed,
return the index at which it was removed from. If s was not removed, return -1.
*/
public int remove(String s){
/*NumElements--;
if((double)NumElements/TableSize<=0.25){
resize(TableSize/2); */
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element)){
T[k]=null;
NumElements--;
if((double)NumElements/TableSize<=0.25)
TableSize=TableSize/2;
resize(TableSize);
}
j++;
k=(i+(j*j))%TableSize;
}
}
/* resize()
Resize the hash table to be a prime within the load factor requirements.
*/
public void resize(int newSize){
while(!isPrime(newSize)){
newSize++;
}
String[] T2= new String[newSize];
T=T2;
}
public boolean isPrime(int num){
if(num>2){
for(int i=2;i if(num%i==0){
return false;
}
return true;
}
}
return false;
}
Explanation / Answer
Yes. I can help you in resolving the error. I will brief you in three different approaches, You can use any one of them.
The problem is with the small.txt file, When you are executing it, It is not matching with the No. of strings and its assuming it contains more string items. Thats the reason its giving an exception. Compile the code.!!!
Approach 1:
import java.util.Scanner;
public class Approach1
{
public static void main(String args[])
{
String str;
Scanner s = new Scanner(System.in);
System.out.print("Enter a String to copy : ");
str = s.nextLine();
System.out.print("Now please Copy the String... ");
StringBuffer strCopy = new StringBuffer(str);
System.out.print("The Given String Copied Successfully..! ");
System.out.print("The Result Copied String is " + strCopy);
}
}
Approach 2:-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Approach2 {
public static void main(String[] args)
{
InputStream inStm = null;
OutputStream outStm = null;
try{
File file1 =new File("Desktop/file1.txt");
File file2 =new File("Desktop/file2.txt");
inStm = new FileInputStream(file1);
outStm = new FileOutputStream(file2);
byte[] buffer = new byte[1024];
int length;
while ((length = inStm.read(buffer)) > 0){
outStm.write(buffer, 0, length);
}
if (inStm != null)inStm.close();
if (outStm != null)outStm.close();
System.out.println("File Copied..");
}catch(IOException e){
e.printStackTrace();
}
}
Approach 3:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Approach3 {
public static void main(String[] args) {
File file = new File("file.txt");
try {
Scanner s = new Scanner(file);
while (s.hasNextLine()) {
int n = s.nextInt();
System.out.println(n);
}
s.close();
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.