0

So basically i have this java project for school, and it requires me to read information from a text file, create an object from that information, and then insert that object into a hash table.

I am able to read the information from the text file and create the object, however, for some reason, my program isnt inserting the object into the hash table. Because then at the end of the program, when i try to search for the object (via its ID), it just returns 'not found'.

EDIT: My question is not how to compare strings. It is how to take the information from the text file, turn it into an object, and then insert it into the hash table. I know how to take the info and turn it into an object, but for some reason, it is not being inserted into the hashtable. Nothing i am inserting into the hashtable from the textfile is actually being inserted. That is the problem I am having.

This is the code i have written:

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());
            }
        }
    }

}

If anyone could help me out with this problem that would be great. I have been looking at this thing for a few days now and cant get it to work.

jmac
  • 79
  • 9
  • 1
    `entry.getID() != idnum` - how to compare Strings in Java – Scary Wombat Apr 13 '18 at 02:35
  • What Wombat is saying: use String's equals method to compare strings. – sn42 Apr 13 '18 at 02:38
  • 1
    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Scary Wombat Apr 13 '18 at 02:39
  • I see what your are saying. Sloppy coding on my part. I fixed it, but it doesnt solve my problem. Any ideas? – jmac Apr 13 '18 at 02:43
  • Did you change your `insert` as well? If so then time for some debugging – Scary Wombat Apr 13 '18 at 02:48
  • Now comes the fun part of programming: debugging. If you need some suggestions on how to get started, [read this](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Kevin Anderson Apr 13 '18 at 02:50
  • I've been trying to debug it. And I know for sure that I can read whats on the text file, and turn it into an object. But for some reason, the object isnt being inserted into the hashtable. – jmac Apr 13 '18 at 02:53
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – David Conrad Apr 13 '18 at 03:17

1 Answers1

0
entry.getID() != idnum

change to

!idnum.equals(entry.getID())

in case of entry.getID() returns null

entry.getID() == idnum

is also the same

==================

update: Since you said the object is not insert into array. I guess it happens here:

There's an id let's say "100" in array. And you want to insert an Product with id "100", so it goes to

    else{
        Product entry = table[hash];
        while((entry.getNext() != null) && (entry.getID() != i)){
            entry = entry.getNext();
        }
        entry.setNext(new Product(n, i, pr));
    }

You use entry.getID() != i or !i.equals(entry.getID(), both are ok. But in the end, you set new Product to entry.next.

But in your searchByID() method. You are trying to search through table[hash]

        while((entry != null) && (entry.getID() != idnum)){
            entry = entry.getNext(); // comment 1
        }
        if(entry == null){
            return "Not Found.";
        }

Issue happens here, entry is null will jump out the loop. It means when while loop is done, entry would always be null. You can set a breakpoint on comment 1.

Bejond
  • 1,188
  • 1
  • 11
  • 18
  • Thanks, I changed that. however, it doesnt solve my problem. – jmac Apr 13 '18 at 02:54
  • also to `entry.getID() != i` – Bejond Apr 13 '18 at 03:09
  • thanks for updating your answer but for some reason it still didnt work. I put a breakpoint on that line you were talking about (by double clicking in the margin) and it still gives me the same result. I still keep getting the same result. I cant tell if its not being inserted into the hashtable properly or if the search method isnt working properly. – jmac Apr 13 '18 at 21:53
  • In debug mode, breakpoint would stop program from running and show you the variables right now. You have time to see the values are desired or not. Breakpoint doesn't modify the code, it won't fix the bug. – Bejond Apr 14 '18 at 02:16