Greetings,
I'm trying to validate whether my integer is null. If it is, I need to prompt the user to enter a value. My background is Perl, so my first attempt looks like this:
我正在尝试验证我的整数是否为空。如果是,我需要提示用户输入值。我的背景是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);
}
This does not work, since Java is expecting boolean logic.
这不起作用,因为Java期望布尔逻辑。
In Perl, I can use "exists" to check whether hash/array elements contain data with:
在Perl中,我可以使用“exists”来检查散列/数组元素是否包含以下数据:
@items = ("one", "two", "three");
#@items = ();
if (exists($items[0])) {
print "Something in \@items.\n";
}
else {
print "Nothing in \@items!\n";
}
Is there a way to this in Java? Thank you for your help!
在Java中有这种方法吗?谢谢您的帮助!
Jeremiah
P.S. Perl exists info.
附: Perl存在信息。
7 个解决方案
#1
41
parseInt()
is just going to throw an exception if the parsing can't complete successfully. You can instead use Integers
, the corresponding object type, which makes things a little bit cleaner. So you probably want something closer to:
如果解析无法成功完成,parseInt()将抛出异常。您可以改为使用相应的对象类型Integers,这样可以使事情变得更加清晰。所以你可能想要更接近的东西:
Integer s = null;
try {
s = Integer.valueOf(startField.getText());
}
catch (NumberFormatException e) {
// ...
}
if (s != null) { ... }
Beware if you do decide to use parseInt()
! parseInt()
doesn't support good internationalization, so you have to jump through even more hoops:
请注意,如果您决定使用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.
}
#2
8
Try this:
Integer startIn = null;
try {
startIn = Integer.valueOf(startField.getText());
} catch (NumberFormatException e) {
.
.
.
}
if (startIn == null) {
// Prompt for value...
}
#3
3
int
s are value types; they can never be null
. Instead, if the parsing failed, parseInt
will throw a NumberFormatException
that you need to catch.
整数是价值类型;他们永远不会是空的。相反,如果解析失败,parseInt将抛出您需要捕获的NumberFormatException。
#4
2
There is no exists
for a SCALAR in Perl, anyway. The Perl way is
无论如何,Perl中没有SCALAR。 Perl的方式是
defined( $x )
and the equivalent Java is
和等效的Java是
anInteger != null
Those are the equivalents.
那些是等价物。
exists $hash{key}
Is like the Java
就像Java一样
map.containsKey( "key" )
From your example, I think you're looking for
从你的例子来看,我认为你正在寻找
if ( startIn != null ) { ...
if(startIn!= null){...
#5
2
For me just using the Integer.toString() method works for me just fine. You can convert it over if you just want to very if it is null. Example below:
对我来说,只使用Integer.toString()方法对我来说很好。如果只是想要它,那么你可以将它转换为null。示例如下:
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 )
#6
0
I don't think you can use "exists" on an integer in Perl, only on collections. Can you give an example of what you mean in Perl which matches your example in Java.
我认为你不能在Perl中的整数上使用“exists”,只能在集合上使用。你能举例说明你在Perl中的意思与Java中的例子相符吗?
Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.
给定一个指定哈希元素或数组元素的表达式,如果哈希值或数组中的指定元素已被初始化,则返回true,即使相应的值未定义。
This indicates it only applies to hash or array elements!
这表明它只适用于散列或数组元素!
#7
0
This should help.
这应该有所帮助。
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
}
#1
41
parseInt()
is just going to throw an exception if the parsing can't complete successfully. You can instead use Integers
, the corresponding object type, which makes things a little bit cleaner. So you probably want something closer to:
如果解析无法成功完成,parseInt()将抛出异常。您可以改为使用相应的对象类型Integers,这样可以使事情变得更加清晰。所以你可能想要更接近的东西:
Integer s = null;
try {
s = Integer.valueOf(startField.getText());
}
catch (NumberFormatException e) {
// ...
}
if (s != null) { ... }
Beware if you do decide to use parseInt()
! parseInt()
doesn't support good internationalization, so you have to jump through even more hoops:
请注意,如果您决定使用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.
}
#2
8
Try this:
Integer startIn = null;
try {
startIn = Integer.valueOf(startField.getText());
} catch (NumberFormatException e) {
.
.
.
}
if (startIn == null) {
// Prompt for value...
}
#3
3
int
s are value types; they can never be null
. Instead, if the parsing failed, parseInt
will throw a NumberFormatException
that you need to catch.
整数是价值类型;他们永远不会是空的。相反,如果解析失败,parseInt将抛出您需要捕获的NumberFormatException。
#4
2
There is no exists
for a SCALAR in Perl, anyway. The Perl way is
无论如何,Perl中没有SCALAR。 Perl的方式是
defined( $x )
and the equivalent Java is
和等效的Java是
anInteger != null
Those are the equivalents.
那些是等价物。
exists $hash{key}
Is like the Java
就像Java一样
map.containsKey( "key" )
From your example, I think you're looking for
从你的例子来看,我认为你正在寻找
if ( startIn != null ) { ...
if(startIn!= null){...
#5
2
For me just using the Integer.toString() method works for me just fine. You can convert it over if you just want to very if it is null. Example below:
对我来说,只使用Integer.toString()方法对我来说很好。如果只是想要它,那么你可以将它转换为null。示例如下:
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 )
#6
0
I don't think you can use "exists" on an integer in Perl, only on collections. Can you give an example of what you mean in Perl which matches your example in Java.
我认为你不能在Perl中的整数上使用“exists”,只能在集合上使用。你能举例说明你在Perl中的意思与Java中的例子相符吗?
Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined.
给定一个指定哈希元素或数组元素的表达式,如果哈希值或数组中的指定元素已被初始化,则返回true,即使相应的值未定义。
This indicates it only applies to hash or array elements!
这表明它只适用于散列或数组元素!
#7
0
This should help.
这应该有所帮助。
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
}