gpt4 book ai didi

java - 检查字段是否为空

转载 作者:行者123 更新时间:2023-12-01 18:00:05 25 4
gpt4 key购买 nike

下面是我的代码,我试图检查 xCoord 是否为空或空,以便我可以抛出异常。我该怎么做?我尝试使用 try/catch if xCoord == null 但这行不通。

String x = JOptionPane.showInputDialog("X Coordinate", "Enter an x coordinate");
int xCoord = Integer.parseInt(x);
String y = JOptionPane.showInputDialog("Y Coordinate", "Enter a y coordinate");
int yCoord = Integer.parseInt(y);
String width = JOptionPane.showInputDialog("Radius", "Enter the length of the radius");
int radius = Integer.parseInt(width);

最佳答案

一旦你有了xCoord,就已经太晚了。在尝试解析它之前,您需要检查x:

String x = JOptionPane.showInputDialog("X Coordinate", "Enter an x coordinate");
if (x == null || x.length() == 0) {
// Throw a meaningful exception
}
int xCoord = Integer.parseInt(x);

关于java - 检查字段是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700640/

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