我如何检查我的字符串是否不是整数,以澄清我需要检查它不是数字
How can I check if my string is other than int's, to clarify i need to check it is not a number
... 如果我得到的不是数字而是字母、符号或两位小数。
我正在 java.
中制定一个变革者计划
一切正常,我唯一感到困惑的是检查字符串以查看是否对我的使用无效,
我这样做是为了什么时候留空;
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid input! ");
System.exit(0);
}
太完美了,现在我该如何 else
检查字母、点或符号,任何不是数字的东西?
您可以使用正则表达式。
下面是一些示例代码,用于仅检查输入字符串中的数字 (\d
)。
实际检查的代码是pattern.matcher(in).matches()
,它试图匹配regex
定义的正则表达式
如果您需要更多解释,请告诉我
public class HelloWorld{
public static void main(String[] args) {
String regex = "\d+";
String inputNumber = "2";
String inputDecimal = "2.0";
String inputString = "two";
String[] inputs = {inputDecimal, inputNumber, inputString };
Pattern pattern = Pattern.compile(regex);
for(String in: inputs){
System.out.print( in + " ");
System.out.print( pattern.matcher(in).matches()? "":"does not");
System.out.print( " contain integer numbers" );
System.out.println("---");
}
}
}
您可以使用 regular expression1 and String.matches(String)
which Tells whether or not this string matches the given regular expression。 \d+
应匹配一位或多位数字。像
System.out.println("12".matches("\d+"));
版画
true
1有些人,遇到问题的时候,会想
“我知道,我会使用正则表达式。”现在他们有两个问题。 --jwz
如果仅当字符串为整数时才需要执行所有处理,为什么不检查 if 子句中的整数值,并让 else 子句对所有字母、点、符号以及空值都是通用的。
if(s1.isNum){
//all processing here
}
else{
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
否则你也可以使用 try 和 catch 块。
try{
int num= Integer.parseInt(s1);
//rest of the processing
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
根据您的要求使用其中一种
要测试它是否是整数,将其解析为 int
,如下所示:
Integer.parseInt(s1)
您可能还想使用返回的值,但我没有在此处显示。现在您可以在方法调用周围应用 try catch 块并像这样捕获 NumberFormatException
:
try {
Integer.parseInt(s1);
//The code here will run if s1 is an integer.
} catch (NumberFormatException e) {
//this will be executed when s1 is not an integer
}
你也可以从上面的代码中提取一个方法。未抛出异常时 returns 为 true 的方法。然而,try catch 的一个缺点是抛出异常需要时间,因此它会减慢你的程序。
要测试字符串是否为字母,您循环遍历字符串中的所有字符并使用 Character
class.
的方法之一
boolean isLetter = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isLetter(s1.charAt(i))) {
isLetter = false;
break;
}
}
如果isLetter
为真,则为字母。同样,您也可以将其提取为方法。
要检查它是否是符号,请使用Character
class 的方法之一(再次)。
boolean isSymb = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isJavaIdentifierStart(s1.charAt(i))) {
isSymb = false;
break;
}
}
要检查字符串中的点,只需使用
s1.contains(".")
是不是很简单?
好的,我通过以下方式解决了这个问题...我采纳了每个想法哈哈...
if (s1 == null) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
for (int i = 0; i < s1.length(); i = i + 1) {
if (!Character.isDigit(s1.charAt(i))) {
JOptionPane.showMessageDialog(null, "You must enter an integer value");
System.exit(0);
}
}
... 如果我得到的不是数字而是字母、符号或两位小数。
我正在 java.
中制定一个变革者计划一切正常,我唯一感到困惑的是检查字符串以查看是否对我的使用无效, 我这样做是为了什么时候留空;
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid input! ");
System.exit(0);
}
太完美了,现在我该如何 else
检查字母、点或符号,任何不是数字的东西?
您可以使用正则表达式。
下面是一些示例代码,用于仅检查输入字符串中的数字 (\d
)。
实际检查的代码是pattern.matcher(in).matches()
,它试图匹配regex
如果您需要更多解释,请告诉我
public class HelloWorld{
public static void main(String[] args) {
String regex = "\d+";
String inputNumber = "2";
String inputDecimal = "2.0";
String inputString = "two";
String[] inputs = {inputDecimal, inputNumber, inputString };
Pattern pattern = Pattern.compile(regex);
for(String in: inputs){
System.out.print( in + " ");
System.out.print( pattern.matcher(in).matches()? "":"does not");
System.out.print( " contain integer numbers" );
System.out.println("---");
}
}
}
您可以使用 regular expression1 and String.matches(String)
which Tells whether or not this string matches the given regular expression。 \d+
应匹配一位或多位数字。像
System.out.println("12".matches("\d+"));
版画
true
1有些人,遇到问题的时候,会想 “我知道,我会使用正则表达式。”现在他们有两个问题。 --jwz
如果仅当字符串为整数时才需要执行所有处理,为什么不检查 if 子句中的整数值,并让 else 子句对所有字母、点、符号以及空值都是通用的。
if(s1.isNum){
//all processing here
}
else{
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
否则你也可以使用 try 和 catch 块。
try{
int num= Integer.parseInt(s1);
//rest of the processing
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Invalid Input");
System.out.exit(0);
}
根据您的要求使用其中一种
要测试它是否是整数,将其解析为 int
,如下所示:
Integer.parseInt(s1)
您可能还想使用返回的值,但我没有在此处显示。现在您可以在方法调用周围应用 try catch 块并像这样捕获 NumberFormatException
:
try {
Integer.parseInt(s1);
//The code here will run if s1 is an integer.
} catch (NumberFormatException e) {
//this will be executed when s1 is not an integer
}
你也可以从上面的代码中提取一个方法。未抛出异常时 returns 为 true 的方法。然而,try catch 的一个缺点是抛出异常需要时间,因此它会减慢你的程序。
要测试字符串是否为字母,您循环遍历字符串中的所有字符并使用 Character
class.
boolean isLetter = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isLetter(s1.charAt(i))) {
isLetter = false;
break;
}
}
如果isLetter
为真,则为字母。同样,您也可以将其提取为方法。
要检查它是否是符号,请使用Character
class 的方法之一(再次)。
boolean isSymb = true;
for (int i = 0 ; i < s1.length() ; i++) {
if (!Character.isJavaIdentifierStart(s1.charAt(i))) {
isSymb = false;
break;
}
}
要检查字符串中的点,只需使用
s1.contains(".")
是不是很简单?
好的,我通过以下方式解决了这个问题...我采纳了每个想法哈哈...
if (s1 == null) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
if (s1.isEmpty()) {
JOptionPane.showMessageDialog(null, "You must enter a valid integer");
System.exit(0);
}
for (int i = 0; i < s1.length(); i = i + 1) {
if (!Character.isDigit(s1.charAt(i))) {
JOptionPane.showMessageDialog(null, "You must enter an integer value");
System.exit(0);
}
}