java - Number Format Exception - converting with parseInt -
i working @ converting string number binary. eclipse throws numberformatexception. can ask you, @ code? have no idea wrong..
public float liczbaf(string lancuch){ float array [] = new float [31]; float liczba; double mantysa; int znak; long cecha; char element[] = new char[22]; string temp=""; if (lancuch.charat(0)=='1') znak=-1; else znak=1; for(int i=1;i<8;i++) { element[i-1] = lancuch.charat(i); } temp=string.valueof(element); system.out.println(temp); cecha=integer.parseint(temp,10); cecha=cecha-127; system.out.println(cecha); for(int i=31;i>9;i--) { element[31-i] = lancuch.charat(i); } temp=string.valueof(element); mantysa=(((integer.parseint(temp,10))/(pow(2,22)))+1); liczba=(float)(mantysa*pow(2,cecha)); return liczba; }
it throws:
exception in thread "main" java.lang.numberformatexception: input string: "1001101 @ java.lang.numberformatexception.forinputstring(unknown source) @ java.lang.integer.parseint(unknown source) @ konwersja.liczbaf(konwersja.java:30) @ main.main(main.java:10)
i grateful help. thank you
your element array 22 long:
char element[] = new char[22];
but fill in first 7 elements:
for(int i=1;i<8;i++) { element[i-1] = lancuch.charat(i); }
so there null characters @ end of string, make unparseable integer. works better:
temp=string.valueof(element,0,7);
i recommend using stringbuilder add characters string, not char array.
Comments
Post a Comment