0
public class HashTable implements IHashTable{

private DataItem[] HashArray;
private int ArraySize;
private DataItem NonItem;
public HashTable(int Size)
{
    this.ArraySize = Size;
    this.HashArray = new DataItem[this.ArraySize];
    this.NonItem = new DataItem(-1);
}
@Override
public void DisplayHashTable() {
    // TODO Auto-generated method stub
    
    System.out.println("The Table Is :\n");
    for(int i =0 ; i < this.ArraySize; i ++)
    {
        if(this.HashArray[i] != null)
            System.out.print(this.HashArray[i] + "  ");
        else
            System.out.println("** ");
    }
    
    System.out.println(" ");
    
}

@Override
public int HashFunc(int Key) {
    // TODO Auto-generated method stub
    
    return Key % this.ArraySize;
}

@Override
public void insert(DataItem item) {
    // TODO Auto-generated method stub
    int Key = item.GetKey();
    int HashVal = HashFunc(Key);
    while (this.HashArray[HashVal] != null
            &&this.HashArray[HashVal].GetKey() != -1)
    {
        ++HashVal;
        HashVal%=this.ArraySize;
    }
    this.HashArray[HashVal] = item;
    // end .
}

@Override
public DataItem Delete(int Key) {
    // TODO Auto-generated method stub
    int HashVal = HashFunc(Key);
    while(this.HashArray[HashVal] != null)
    {
        if(this.HashArray[HashVal].GetKey()== Key)
        {
            DataItem Temp = this.HashArray[HashVal];
            this.HashArray[HashVal] = this.NonItem;
            return Temp;
        }
        ++HashVal;
        HashVal %= this.ArraySize;
    }
    return null;
}

@Override
public DataItem Find(int Key) {
    // TODO Auto-generated method stub
    int HashVal = HashFunc(Key);
    while(this.HashArray[HashVal] != null)
    {
        if(this.HashArray[HashVal].GetKey() == Key)
            return this.HashArray[HashVal];
        ++HashVal;
        HashVal %= this.ArraySize;
    }
    return null;
}

}`


import java.util.*; public class HashTableTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    int Key ;
    DataItem aDataItem;
    IHashTable HashTable = new HashTable(5);
    Menu();
    int ans =sc.nextInt();
    while(ans !=5)
    {
        
        switch(ans){
        case 1 :
            HashTable.DisplayHashTable();
            break;
        case 2 :
            Key = sc.nextInt();
            aDataItem = new DataItem(Key);
            HashTable.insert(aDataItem);
            System.out.println("The Item Has Inserted");
            break;
        case 3 :
            System.out.println("What Value You want to delete");
            Key = sc.nextInt();
            HashTable.Delete(Key);
            break;
        case 4 :
            System.out.println("What Value You want to Find");
            Key = sc.nextInt();
            aDataItem=HashTable.Find(Key);
            if(aDataItem != null)
                System.out.println("Found Key");
            System.out.println("Could not Found" + Key);
            break;
            default :
                System.out.println("Wrong Number");
        }
        Menu();
         ans =sc.nextInt();
        
    }
    
    
}
public static void Menu()
{
    System.out.println("1 - Show The Table \n 2- Insert New Value \n"
            +"3- Delete A Value \n 4- Find A Value\n 5- Exit.");
}

}

// the output

1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. 1 The Table Is :

** ** ** ** **

1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. 2 254 The Item Has Inserted 1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. 2 75 The Item Has Inserted 1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. 2 5 The Item Has Inserted 1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. 1 The Table Is :

DataItem@42d3bd8b DataItem@26ba2a48 ** ** DataItem@5f2050f6
1 - Show The Table 2- Insert New Value 3- Delete A Value 4- Find A Value 5- Exit. `

Murtaja
  • 15
  • 3
  • 1
    You printed out a bunch of `DataItem`s, not integers. `DataItem` isn't shown in the snippets above, so I presume it's a class of your own design – Silvio Mayolo Dec 07 '21 at 03:01

1 Answers1

0

Your HashArray is an array of DataItems. When you call

System.out.print(this.HashArray[i] + "  ");

inside the for loop in the DisplayHashTable method, you're basically calling the DataItem at index i and printing it. Since that is an object, its address in memory (the "nonsense numbers") is being printed to the terminal.

One way to fix this would be to add a method inside the DataItem class:

public void toString()
{
    //print whatever you want to output here
}

This should work according to what you want.

TheSj
  • 376
  • 1
  • 11