![](/img/trans.png)
[英]java.lang.NumberFormatException: For input string: "", while not converting a string into a number
[英]java.lang.NumberFormatException: Converting string to ASCII
我正在嘗試通過哈希函數將字符串轉換為ASCII值,如下所示:
public long hash(String word){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for(int i=0;i<word.length();i++){
sb.append((int)word.charAt(i));
}
ascString = sb.toString();
asciiInt = Long.parseLong(ascString);
return asciiInt;
}
然后,我將在我的insert()方法中調用它,以使用hashTable執行二次哈希,並且insert方法如下所示:
public void insert(Word word){
int start = (int)(hash(word.text)%tableSize);
int key = start;
int attempt=0;
while(hashTable[key]!=null){
attempt++;
key=(start+(int)Math.pow(attempt,2))%tableSize;
}
hashTable[key]=word;
}
但是,如果我嘗試轉換的字符串超過6個字符,它將引發java.lang.NumberFormatException
。 誰能幫我解決這個問題,或者提供一種更好的方法為我的哈希表提供鍵值?
謝謝!
您嘗試從字符串中獲取的值(以10為基數)無法獲得,因為您的基數錯誤。 假設字符串為“ DEADBEEF”。 因為DEADBEEF的所有數字都以16為底,所以您可以將基數指定為16並使用
Long.parseLong(DEADBEEF, 16);
非基數方法假定字符串中的數字實際上更長(當以10為底數時,DEADBEEF為3735928559),該字符串包含以10為底的長字符串。 檢查您的字符串也許嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.