无法从哈希表存储和检索值

时间:2019-05-28 18:57:52

标签: java

我正在从记事本中读取带有以下信息的TXD文件

aerial=airy*
banish=exile*
clingy=adhesive*
distinct=clear*
elusive=fugitive*
frustration=depair*
galand=brave*
hero=champion*
important=significant*
jinx=curse*
karma=revenge*
link=connection*

现在,在加法器方法中,我读取每一行,直到“ ”。到达“ ”后,我将在哈希表中发送该行的第一个字符(第一个字符= key,即天线=> a = key,我使用该字符的ascii代码存储该密钥),并且该行的其余部分,直到“ *”存储在哈希表的数据部分中为止。在main中调用adder()和Gethash1()方法之后。我无法从哈希表中检索值。值未正确存储或可能未正确检索。请花点时间。这将是巨大的帮助

package testing;
import java.util.Arrays;
import java.util.Scanner;


public class hashtable {
Reader r1=new Reader();
String words1=r1.get(); //String in which the reader string is stored
int tableSize = 150;

Node [] table = new Node[tableSize];

static class Node{
String val;
char key;
Node next;

}







 //Funtion to store values in the nodes of the array.
    void Put(char kk, String vv){
    int location=0;
    int sum=0;
    int rem=0;
    sum=(int)kk;    

    rem=sum%tableSize;
    location=rem;
    Node newNode;
    newNode= new Node();
    newNode.key=kk;
    newNode.val=vv;
    if(table[location]==null){
    table[location]=newNode;
    }
    else{
    Node trv;
    trv=table[location];
    Node PrevNode = new Node();
    while(trv!=null){
    PrevNode=trv;
    trv=trv.next;
    }
    trv=newNode;
    PrevNode.next=trv;
    }

    }

    // funtion to add data in to hashtable
     void adder(){   
     String con=""; //concatinated characters
     String strd="";//Stored string after each *
     char stor=0; //stores charater. it is mainly used to find the first             
     character of the sentence
     for(int i =0; i<words1.length(); i++){       
     char w1=words1.charAt(i);    
     if(w1=='*'){    
     con=""; // To make it zero every time we encounter *
     if(((i+1)!=words1.length())){  
      stor=words1.charAt(i+1);   
     }
     }
     else{       
     con=con.concat(String.valueOf(w1));
     }
      if(i+1!=words1.length()){
      if((words1.charAt(1+i))=='*'){
      strd=con;
      Put(stor, strd);
      }}  
     }
     }



     //Function to get the values of the hashtable
     void Gethash1(){
     System.out.println("Enter the word you want to find: ");
    Scanner scann=new Scanner(System.in);
    String getword="";
    getword=scann.nextLine();
    char getlet='0';
    int key=(int)getlet;
    int length=getword.length(); //lenght of the word
    Node tvr;
    tvr=table[key];
    while(tvr!=null){

            for(int i=0; i<length; i++){
                if(length!=i){
            if(getword.charAt(i)==tvr.val.charAt(i)){
            break;
            }
                }
            }
            System.out.println("true");   
           tvr=tvr.next;
    }

     }


    void getLinkedListSize(Node linkedList){
        int count = 0;
        while (linkedList!=null){
            linkedList = linkedList.next;
            count++;
        }
    }

0 个答案:

没有答案