繁体   English   中英

如何判断Java整数是否为空?

[英]How can I tell if a Java integer is null?

问候,

我正在尝试验证我的整数是否为空。 如果是,我需要提示用户输入值。 我的背景是Perl,所以我的第一次尝试看起来像这样:

int startIn = Integer.parseInt (startField.getText());

if (startIn) { 
    JOptionPane.showMessageDialog(null,
         "You must enter a number between 0-16.","Input Error",
         JOptionPane.ERROR_MESSAGE);                
}

这不起作用,因为Java期望布尔逻辑。

在Perl中,我可以使用“exists”来检查散列/数组元素是否包含以下数据:

@items = ("one", "two", "three");
#@items = ();

if (exists($items[0])) {
    print "Something in \@items.\n";
}
else {
    print "Nothing in \@items!\n";
}

在Java中有这种方法吗? 谢谢您的帮助!

耶利米

PS Perl 存在信息。

如果解析无法成功完成, parseInt()将抛出异常。 您可以改为使用相应的对象类型Integers ,这样可以使事情变得更加清晰。 所以你可能想要更接近的东西:

Integer s = null;

try { 
  s = Integer.valueOf(startField.getText());
}
catch (NumberFormatException e) {
  // ...
}

if (s != null) { ... }

请注意,如果您决定使用parseInt() parseInt()不支持良好的国际化,所以你必须跳过更多的箍:

try {
    NumberFormat nf = NumberFormat.getIntegerInstance(locale);
    nf.setParseIntegerOnly(true);
    nf.setMaximumIntegerDigits(9); // Or whatever you'd like to max out at.

    // Start parsing from the beginning.
    ParsePosition p = new ParsePosition(0);

    int val = format.parse(str, p).intValue();
    if (p.getIndex() != str.length()) {
        // There's some stuff after all the digits are done being processed.
    }

    // Work with the processed value here.
} catch (java.text.ParseFormatException exc) {
    // Something blew up in the parsing.
}

试试这个:

Integer startIn = null;

try {
  startIn = Integer.valueOf(startField.getText());
} catch (NumberFormatException e) {
  .
  .
  .
}

if (startIn == null) {
  // Prompt for value...
}

int是值类型; 他们永远不会是null 相反,如果解析失败, parseInt将抛出您需要捕获的NumberFormatException

in Perl, anyway. 有没有exists于Perl中的 ,反正。 Perl的方式是

defined( $x ) 

和等效的Java是

anInteger != null

那些是等价物。

exists $hash{key}

就像Java一样

map.containsKey( "key" )

从你的例子来看,我认为你正在寻找

if(startIn!= null){...

对我来说,只使用Integer.toString()方法对我来说很好。 如果它只是想要非常,那么你可以转换它。 示例如下:

private void setCarColor(int redIn, int blueIn, int greenIn)
{
//Integer s = null;
if (Integer.toString(redIn) == null || Integer.toString(blueIn) == null ||     Integer.toString(greenIn) == null )

这应该有所帮助。

Integer startIn = null;

// (optional below but a good practice, to prevent errors.)
boolean dontContinue = false;
try {
  Integer.parseInt (startField.getText());
} catch (NumberFormatException e){
  e.printStackTrace();
}

// in java = assigns a boolean in if statements oddly.
// Thus double equal must be used. So if startIn is null, display the message
if (startIn == null) {
  JOptionPane.showMessageDialog(null,
       "You must enter a number between 0-16.","Input Error",
       JOptionPane.ERROR_MESSAGE);                            
}

// (again optional)
if (dontContinue == true) {
  //Do-some-error-fix
}

我认为你不能在Perl中的整数上使用“exists”,只能在集合上使用。 你能举个例子说明你在Perl中与Java中的例子相匹配的意思。

给定一个指定哈希元素或数组元素的表达式,如果哈希或数组中的指定元素已被初始化,则返回true,即使相应的值未定义。

这表明它只适用于散列或数组元素!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM