gpt4 book ai didi

java - 寻找更好的方法来执行以下操作

转载 作者:行者123 更新时间:2023-12-01 11:10:27 25 4
gpt4 key购买 nike

我需要解析大量字符串,确定它们是 int 还是 double。这是我当前可以使用的代码。我希望能够简单地使用现有的两个 try/catch 来保持代码整洁,而不是为所有字符串创建一个。

void camTestButton(ActionEvent event) throws IOException, InterruptedException {

String cam01 = Cam0GO1.getText();
String cam02 = Cam0GO2.getText();
String cam03 = Cam0GO3.getText();
String cam11 = Cam1GO1.getText();
String cam12 = Cam1GO2.getText();
String cam13 = Cam1GO3.getText();

try {
int textToInt = Integer.parseInt(cam01);

} catch (NumberFormatException e) {
try {
double textToDouble = Double.parseDouble(cam01);
} catch (NumberFormatException e2) {

}
}
System.out.println(cam01);
}

我现在可以正常工作了,但我需要获取结果并返回到每个单独的文本字段?

    String cam01 = Cam0GO1.getText();
String cam02 = Cam0GO2.getText();
String cam03 = Cam0GO3.getText();
String cam11 = Cam1GO1.getText();
String cam12 = Cam1GO2.getText();
String cam13 = Cam1GO3.getText();
String[] cams = {cam01, cam02, cam03, cam11, cam12, cam13};

for (String c : cams) {
try {
int textToInt = Integer.parseInt(c);

} catch (NumberFormatException e) {
try {
double textToDouble = Double.parseDouble(c);
} catch (NumberFormatException e2) {

}
}
**System.out.println("Cam0GO01 = " + c.?? etc.);**
}

最佳答案

void camTestButton(ActionEvent event) throws IOException,InterruptedException {
String [] array = {Cam0GO1.getText(), Cam0GO2.getText(), Cam0GO3.getText()};
for (String st : array) {
parseCam(st);
}
}

public void parseCam(String st) {
try {
int textToInt = Integer.parseInt(st);
} catch (NumberFormatException e) {
try {
double textToDouble = Double.parseDouble(st);
} catch (NumberFormatException e2) {}
}
System.out.println(st);
}

关于java - 寻找更好的方法来执行以下操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32445396/

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