0
我想寫一個程序,將積極的二進制輸入轉換爲十六進制。 爲什麼會出現這樣的錯誤,而我的編譯二進制到十六進制轉換器..Java.lang.NumberFormatException異常拋出
Exception in thread "main" java.lang.NumberFormatException: For input string: "148.0"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at BinToHex.convertbintohex(BinToHex.java:24)
at Test.main(Test.java:4)
這裏是我BinToHex類
import java.io.*;
public class BinToHex {
double tempDec,fractionpart;
long longofintpart,templongDec;
String inpu ="1001.01";
String hexOutput,intpart,tempDecString,hex = null;
static int i = 1;
public void convertbintohex() {
if (inpu.contains(".")) {
int placesAfterPoint = inpu.length() - inpu.indexOf(".") - 1;//every thing
long numerator = Long.parseLong(inpu.replace(".", ""), 2);//goes
double decimalOfInput = ((double) numerator)/(1L << placesAfterPoint);//alright till here
while (true) {
tempDec = decimalOfInput * 16;
if ((int)tempDec == tempDec) {
tempDecString = String.valueOf(tempDec);
templongDec = Long.parseLong(tempDecString, 10);
hexOutput = Long.toHexString(templongDec);
break;
} else {
intpart = String.valueOf((long)tempDec);
longofintpart = Long.valueOf(intpart).longValue();
if(i==1){
hex=Long.toHexString(longofintpart);
hexOutput = hex + ".";
i=i+1;
}else{
hexOutput = hexOutput + hex;
}
fractionpart = tempDec-(int)tempDec;
decimalOfInput = fractionpart;
}
}
} else {
// this part is ok
tempDecString = String.valueOf(Integer.parseInt(inpu, 2));
templongDec = Long.parseLong(tempDecString, 10);
hexOutput = Long.toHexString(templongDec);
}
System.out.println(hexOutput);
}
}
和我的測試類..
public class Test{
public static void main(String args[]){
BinToHex i = new BinToHex();
i.convertbintohex();
}
}
對不起,這樣的問題;-) 真正需要幫助的
這不是一個編譯器錯誤。 – 2014-09-29 14:40:48
String'148.0'不能被解析爲'Long'。 – 2014-09-29 14:41:07
爲什麼不能將字符串解析成長整型?這是什麼解決方案? @SotiriosDelimanolis – Tiash 2014-09-29 14:43:02