所以基本上我有这个学校的java项目,它要求我从文本文件中读取信息,从该信息创建一个对象,然后将该对象插入到哈希表中。
我能够从文本文件中读取信息并创建对象,但是,出于某种原因,我的程序没有将对象插入到哈希表中。因为在程序结束时,当我尝试搜索对象(通过其ID)时,它只返回“未找到”#39;。
编辑:我的问题不是如何比较字符串。它是如何从文本文件中获取信息,将其转换为对象,然后将其插入哈希表。我知道如何获取信息并将其转换为对象,但由于某种原因,它没有被插入到哈希表中。我从textfile插入哈希表中的任何内容实际上都没有插入。这就是我遇到的问题。
这是我写的代码:
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class testmain {
public static void main(String[] args) throws FileNotFoundException{
StorageSystem ss = new StorageSystem();
ss.createhashtable();
String line;
String [] parts;
File in = new File("database.txt");
Scanner sc = new Scanner(in);
while(sc.hasNextLine()){
line = sc.nextLine();
parts = line.split(" ");
String type = parts[0];
String name = parts[1];
String id = parts[2];
String price = parts[3];
if(type.equals("Insert")){
Product p1 = new Product(name, id, Double.parseDouble(price));
ss.insert(p1);
}
else if(type.equals("remove")){
ss.remove(id);
}
}
sc.close();
System.out.println(ss.searchbyID("123"));
System.out.println(ss.searchbyID("232"));
System.out.println(ss.searchbyID("444"));
System.out.println(ss.searchbyID("456"));
//If i do it manually (below), it works. But thats not the point of this project
/*
Product ex = new Product("joe", "123", 22.33);
ss.insert(ex);
System.out.println(ss.searchbyID("123"));
ss.remove("123");
System.out.println(ss.searchbyID("123"));
*/
//When i do it this way ^^, it inserts the object into the hashtable, it
//searches for the object and finds it. it then removes the object. and
//when it searches again it doesnt find anything. And thats how it
//should work.
}
}
//Below is the class that contains the information on the hashtable
import java.io.*;
import java.util.Scanner;
public class StorageSystem {
private final static int tablesize = 1000;
private static Product[] table = new Product[tablesize];
public StorageSystem(){
table = new Product[tablesize];
for(int i = 0; i < tablesize; i++){
table[i] = null;
}
}
public void createhashtable(){
table = new Product[tablesize];
for(int i = 0; i < tablesize; i++){
table[i] = null;
}
}
public void useExistingTable(Product[] pt){
table = pt;
}
public String searchbyID(String idnum){
int hash = (Integer.parseInt(idnum) % tablesize);
if(table[hash] == null){
return "Not Found.";
}
else{
Product entry = table[hash];
while((entry != null) && (entry.getID() != idnum)){
entry = entry.getNext();
}
if(entry == null){
return "Not Found.";
}
else
return entry.toString();
}
}
public void insert(Product p){
String n = p.getName();
String i = p.getID();
double pr = p.getPrice();
int hash = (Integer.parseInt(i) % tablesize);
if(table[hash] == null){
table[hash] = new Product(n, i, pr);
}
else{
Product entry = table[hash];
while((entry.getNext() != null) && (entry.getID() != i)){
entry = entry.getNext();
}
entry.setNext(new Product(n, i, pr));
}
}
public void remove(String idnum) {
int hash = (Integer.parseInt(idnum) % tablesize);
if (table[hash] != null) {
Product prevEntry = null;
Product entry = table[hash];
while (entry.getNext() != null && entry.getID() != idnum) {
prevEntry = entry;
entry = entry.getNext();
}
if (entry.getID() == idnum) {
if (prevEntry == null)
table[hash] = entry.getNext();
else
prevEntry.setNext(entry.getNext());
}
}
}
}
如果有人能帮助我解决这个问题,那就太棒了。我一直在看这件事几天,不能让它发挥作用。
答案 0 :(得分:0)
entry.getID() != idnum
更改为
!idnum.equals(entry.getID())
如果是entry.getID()则返回null
entry.getID() == idnum
也是一样的
==================
更新: 既然你说对象没有插入到数组中。我猜它发生在这里:
有一个id让我们在数组中说“100”。并且您想插入ID为“100”的产品,因此转到
else{
Product entry = table[hash];
while((entry.getNext() != null) && (entry.getID() != i)){
entry = entry.getNext();
}
entry.setNext(new Product(n, i, pr));
}
您使用entry.getID() != i
或!i.equals(entry.getID()
,两者都可以。但最后,您将新产品设置为entry.next。
但是在你的searchByID()方法中。您正在尝试搜索表[哈希]
while((entry != null) && (entry.getID() != idnum)){
entry = entry.getNext(); // comment 1
}
if(entry == null){
return "Not Found.";
}
问题发生在这里,entry为null将跳出循环。这意味着当while循环完成时,entry将始终为null。您可以在注释1上设置断点。