gpt4 book ai didi

java - 无法将 String 解析为 int

转载 作者:行者123 更新时间:2023-12-01 21:19:06 25 4
gpt4 key购买 nike

我不知道在我的代码中将字符串解析为 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");
}
}
}

最佳答案

您没有将 trim() 返回的 String 存储在任何地方。你可以这样做:

s2 = s2.trim();
int newI = Integer.parseInt(s2);

int newI = Integer.parseInt(s2.trim());

关于java - 无法将 String 解析为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com