如何解决我需要读取并加载一个文本文件并将元素添加到哈希表中;相反,用户输入数据
我需要帮助来更改测试驱动程序。可以看到当前程序要求用户添加信息来存储元素,然后显示表格。我需要的是读取文本文件并加载元素,将元素添加到表格中并在表格中显示它们。这是一个使用单向链表/哈希表/链接的练习。正如我所说,如果您查看输出;输出运行正常,但以创建的方式我需要输入或复制并粘贴单词。我需要读取file.txt,加载它,然后将元素插入表中。
public class HashTable {
// array of linked list of Strings
private LinkedList<String> table[];
// constructor to initialize the array
public HashTable() {
// initializing array with capacity 13
table = new LinkedList[13];
}
// method to insert an item to the table
public void insert(String item) {
// finding hash
int index = hash(item);
// if the linked list at location index is not initialized,initializing it
if (table[index] == null) {
table[index] = new LinkedList<String>();
}
// adding this item to the linked list. replace with your linked list's
// insert method if it differs from 'add'
table[index].add(item);
}
// method to display the indices and the elements in linked lists at each indices
public void displayTable() {
// looping through table
for (int i = 0; i < table.length; i++) {
// displaying index
System.out.printf("%2d : ",i);
// if current list is not initialized,displaying []
if (table[i] == null) {
System.out.println("[]");
} else {
// assuming toString method is defined for the linked list,// displaying list contents
System.out.println(table[i]);
}
}
}
// method to hash a String
private int hash(String str) {
// initializing sum to 0
int asciiSum = 0;
// loops through the first three letters (or the available letters if
// the length is less than 3)
for (int i = 0; i < 3 && i < str.length(); i++) {
// adding ASCII value of current character to sum total
asciiSum += str.charat(i);
}
// finding hash and returning it
int hash = asciiSum % table.length;
return hash;
}
/**
* for the ease of submission,I'm attaching the driver program within this
* class itself. You can move the below main method to whatever the class
* you need,it will work.
*/
public static void main(String[] args) {
// defining a Scanner,a HashTable and reading words until 'done' is entered
System.out.println("Enter Strings to add (type 'done' to finish)");
//File file = new File("file.txt"); //
//Scanner input = new Scanner(file); //
HashTable table = new HashTable();
String input = "";
while (!input.equalsIgnoreCase("done")) {
input = scanner.next();
if (!input.equalsIgnoreCase("done")) {
//adding to table
table.insert(input);
}
}
//closing scanner
scanner.close();
//displaying hash table
System.out.println("Hash Table:");
table.displayTable();
}
}
//OUTPUT
Enter Strings to add (type 'done' to finish)
DEAR MARLIN
THE AARDVARKS AND THE CAMELS WERE MISTAKENLY SHIPPED TO THE AZORES
SORRY ABOUT THAT
SINCERELY JIM
PS ANTS AND BATS AND COWS AND CATS ARE ANIMALS
COWS ARE BIG BUT ANTS ARE SMALL AND BATS AND CATS ARE IN BETWEEN
done
Hash Table:
0 : [AZORES,THAT,SINCERELY]
1 : [CAMELS,BUT]
2 : [ABOUT,BIG]
3 : [MARLIN,AND,JIM,AND]
4 : [THE,AARDVARKS,THE,WERE,SMALL]
5 : []
6 : [ANTS,ANTS]
7 : [DEAR,SHIPPED,TO,PS,BATS,BATS]
8 : [CATS,ARE,ANIMALS,CATS,IN]
9 : []
10 : [SORRY]
11 : [BETWEEN]
12 : [MISTAKENLY,COWS,COWS]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。