[英]How do I make my code check that the user has input a alphabetical character? [Java]
Update : the code works now. 更新 :代码现在可以使用了。 Thanks for help guys.
感谢您的帮助。
This is part of the code I'm having trouble with 这是我遇到麻烦的代码的一部分
if ((amount < 0) || (Character.isLetter(amount)))
Anyone know what I'm doing wrong? 有人知道我在做什么错吗? Here's a piece of my code:
这是我的一段代码:
try
{
if ( theStock.exists( pn ) ) // Button CHECK
{
thePicture.clear();
if ( actionIs.equals( Name.ADD ) ) {
try
{
amount = Integer.parseInt(theInputPQ.getText()); // Checks that
if ((amount < 0) || (Character.isLetter(amount))) { // number has been
theOutput.setText("Please enter sufficient No."); // input
}
else {
theStock.addStock(pn, amount);
Product pr = theStock.getDetails( pn ); // Add to
theAction.setText( // stock of
pr.getDescription() + " : " + // item
theMoney.format(pr.getPrice()) +
" (" + pr.getQuantity() + ")" );
}
}
catch (NumberFormatException e) {
theOutput.setText("Please enter sufficient No.");
Product pr = theStock.getDetails( pn );
theAction.setText(
pr.getDescription() + " : " +
theMoney.format(pr.getPrice()) +
" (" + pr.getQuantity() + ")" );
if ( actionIs.equals( Name.ADD ) ) {
}
}
} else { // F
theAction.setText( // Inform Unknown
"Unknown product number " + pn ); // product number
}
}
You're checking if an int
is a letter or not: 您正在检查
int
是否为字母:
Character.isLetter(amount)
If that int is for instance 65
that method would return true, because what in ascii code the number 65
represents the letter a
如果该int例如是
65
,则该方法将返回true,因为在ascii代码中,数字65
代表字母a
This is probably the source of your problems. 这可能是您问题的根源。
You may safely remove that validation, because, if after the call to Integer.parseInt
you may be sure that amount
is a number ( otherwise it will go to the catch( NumberFormatException )
section below as you may already have noticed. 您可以放心地删除该验证,因为,如果在调用
Integer.parseInt
之后,您可以确定amount
是一个数字(否则,您可能已经注意到,它会转到下面的catch( NumberFormatException )
部分。
Is amount
a int
variable? amount
是int
变量吗? if it is, how it can be a letter ( Character
)? 如果是的话,怎么能是字母(
Character
)? if not int
, then how it can be compared using < 0
? 如果不是
int
,那么如何使用< 0
进行比较? It can be or character or integer, not both above. 它可以是或字符或整数,但不能同时是以上两者。
Well I think that you are already checking if amount is an int by the: 好吧,我认为您已经在通过以下方式检查金额是否为整数:
Integer.parseInt(theInputPQ.getText());
and the try-catch block. 和try-catch块。 don't need to do the
不需要做
Character.isLetter(amount)
The amount < 0
check isn't doing anything useful, unless you are checking for integer values less than 0. Integer.parseInt()
returns the integer that it read, as a string. amount < 0
检查没有任何用处,除非您要检查小于0的整数值Integer.parseInt()
以字符串形式返回其读取的整数。 It throws a NumberFormatException
if its argument does not contain a parseable integer. 如果其参数不包含可解析的整数,则将引发
NumberFormatException
。
I would call a different method if your goal is just to figure out if the button's text has letters in it. 如果您的目标只是弄清楚按钮的文本中是否包含字母,我会调用其他方法。 Also, I'm not sure why you're calling
isLetter()
on the text of a String. 另外,我不确定为什么要在String的文本上调用
isLetter()
。 Here's an example I saw that goes character-by-character: 这是我看到的一个接一个字符的示例:
String str = theInputPQ.getText(); // I assume this is a String
for(int i=0; i < str.length(); i++)
{
if(Character.isDigit(str.charAt(i)))
System.out.println("It's a digit");
if(Character.isLetter(str.charAt(i)))
System.out.println("It's a letter");
}
In the interest of good coding practices, if you add this code then it should be a separate private method. 为了良好的编码习惯,如果添加此代码,则它应该是单独的私有方法。
Sources: Java API for Integer , Dream in Code 来源: Java API for Integer , 代码中的梦想
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.