-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableRunner.java
More file actions
48 lines (46 loc) · 1.41 KB
/
HashTableRunner.java
File metadata and controls
48 lines (46 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.LinkedList;
import java.util.Scanner;
import java.io.File;
import static java.lang.System.*;
import java.util.*;
public class HashTableRunner
{
public static void main ( String[] args )
{
/**
Createing scanner to read the numbers.dat file
*/
String filePath = "numbers.dat";
File file = new File(filePath);
int bucketNum = 10;
//Scanner bum = new Scanner(file);
try(Scanner bum = new Scanner(file)){
System.out.println("LOOKING FOR FILE...");
//make a new table
Hashtable<Integer, LinkedList<Integer>> hashTab = new Hashtable<>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < bucketNum; i++){
hashTab.put(i, new LinkedList<>());
}
//read from the file
while (bum.hasNextLine()){
String line = bum.nextLine().trim();
//ignoring empty lines
if (!line.isEmpty()){
int lum = Integer.parseInt(line);
int bucketInd = lum % bucketNum; // Determine bucket
hashTab.get(bucketInd).add(lum);
}
}
System.out.println("HashTable Buckets:");
for (int j = 0; j < bucketNum; j++) {
System.out.println("Bucket " + j + ": " + hashTab.get(j));
}
}
catch(Exception e)
{
System.out.println("Houston, we have a problem!");
System.out.println(e.getMessage());
}
}
}