我不知道在我的代碼中將字符串解析爲int部分時出了什麼問題。在解析之前,一切看起來都正確。無法將字符串解析爲int
import java.io.IOException;
public class TheWindow {
public static void main(String[] args) throws IOException{
String s = "13.16";
double d = Double.parseDouble(s);
System.out.println(d);
char[] ch = s.toCharArray();
char[] ch2 = new char[s.length()];
for(int i = 0; i < ch.length; i++){
if(ch[i] == '.'){
break;
}else{
ch2[i] = ch[i];
}
}
String s2 = new String(ch2);
System.out.println(s2);
try{
s2.trim();
int newI = Integer.parseInt(s2);
System.out.println(newI);
}catch(Exception e){
System.out.println("Failed");
}
}
}
是什麼錯誤消息說?如果您想知道出了什麼問題,最好閱讀它而不是丟棄它。我懷疑你的'ch2'結尾有一些空字符,你打破了循環,但是你不會讓'ch2'變小。你可以使用你的調試器來遍歷代碼,找出每一行正在做什麼以及哪裏出錯。 –