27

问候,

我正在尝试验证我的整数是否为空。如果是,我需要提示用户输入一个值。我的背景是 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存在信息。

4

7 回答 7

41

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.
}
于 2009-03-14T14:52:32.940 回答
8

试试这个:

Integer startIn = null;

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

if (startIn == null) {
  // Prompt for value...
}
于 2009-03-14T14:58:06.950 回答
3

ints 是值类型;他们永远不可能null。相反,如果解析失败,parseInt将抛出一个NumberFormatException你需要捕获的。

于 2009-03-14T14:53:31.173 回答
2

exists无论如何,Perl中没有SCALAR。Perl的方式是

defined( $x ) 

等效的Java是

anInteger != null

这些是等价物。

exists $hash{key}

就像Java

map.containsKey( "key" )

从您的示例中,我认为您正在寻找

if ( startIn != null ) { ...

于 2009-03-14T19:02:44.150 回答
2

对我来说,只需使用 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 )
于 2012-03-15T19:34:57.550 回答
0

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

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

这表明它仅适用于哈希或数组元素!

于 2009-03-14T15:36:11.057 回答
0

这应该会有所帮助。

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
}
于 2012-07-12T22:48:23.863 回答