验证输入只能是字符串和数字 JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23905985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validation input to be string only and numbers only JAVA
提问by user3682556
I am a student and i have a little problem with validation inputs.
我是一名学生,我在验证输入方面遇到了一些问题。
String name;
System.out.println("Enter name:");
name = keyboard.nextLine();
//If name contain other than alphabetical character print an error
double number;
System.out.println("Enter number:");
name = keyboard.nextDouble();
//If number contain other than number print an error
What i have tried is test to parse the string to double but i could not. I have no idea how to test if the double is only number.
我尝试过的是测试将字符串解析为双倍但我不能。我不知道如何测试 double 是否只是数字。
Please give me a clue of what i should do.
请给我一个我应该做什么的线索。
回答by Christian
You can loop through each character of the String
and check if it's not alphabetic using Character.isAlphabetic(char)
:
您可以循环遍历 的每个字符String
并使用以下方法检查它是否不是字母Character.isAlphabetic(char)
:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name:");
String name = keyboard.nextLine();
for (char c : name.toCharArray()) {
if (!Character.isAlphabetic(c)){
System.out.println("INVALID");
break;
}
}
To only accept numbers, you can do something similar using the Character.isDigit(char)
function, but note that you will have to read the input as a String
not a double
, or get the input as a double
and the converting it to String
using Double.toString(d)
.
要只接受数字,您可以使用该Character.isDigit(char)
函数执行类似的操作,但请注意,您必须将输入读取为String
not a double
,或者将输入作为 a读取double
并将其转换为String
using Double.toString(d)
。
回答by lpratlong
double number = 0;
try {
number = Double.parseDouble(name)
} catch (NumberFormatException ex) {
System.out.println("Name is not a double.");
}
If number
is not a double, you can catch a NumberFormatException
.
如果number
不是双精度型,则可以捕获NumberFormatException
.
回答by anirudh
It seems you are using scanner
. If you are, you can use the Scanner
class' hasNextDouble()
to check if the input is a double before reading the double as shown below:
看来您正在使用scanner
. 如果是,您可以使用Scanner
class'hasNextDouble()
在读取双精度值之前检查输入是否为双精度值,如下所示:
double number;
System.out.println("Enter number:");
if (keyboard.hasNextDouble()){
name = keyboard.nextDouble();
}
Have a look at the Scanner
class docsfor more information.
查看Scanner
课程文档以获取更多信息。
回答by Mifmif
You can use Regular expressionto check if the input match your constraint as follow :
您可以使用正则表达式来检查输入是否符合您的约束,如下所示:
String name;
System.out.println("Enter name:");
name = keyboard.nextLine();
if (!name.matches("[a-zA-Z_]+")) {
System.out.println("Invalid name");
}
String number;
System.out.println("Enter number:");
number = keyboard.nextLine();
if (!number.matches("[0-9]+")) {
System.out.println("Invalid number");
}
Here isa good tutorial to learn regex .
这是学习正则表达式的好教程。
回答by user2007447
There is also the "fancier" regex solution. Java Pattern Documentation
还有“更高级”的正则表达式解决方案。 Java 模式文档
You can use this (untested code) given you used nextLine() for reading BOTH inputs:
鉴于您使用 nextLine() 读取两个输入,您可以使用此(未经测试的代码):
boolean isWordOnly = Pattern.matches("\w*", name); //name is in your code
boolean isFloatOnly = Pattern.matches("\d*.?\d*", number); //number is in your code too
Now the too boolean values tell you if there is the desired format in your input. You can add it in a do - while loop or anything you want.
现在,too 布尔值会告诉您输入中是否有所需的格式。您可以在 do-while 循环或任何您想要的内容中添加它。
It is a good idea to start studying reg(ular) ex(presions) because they are useful in string formatting (Imagine that you have to test if the input is a valid email...). Also used to check for SQL injections and many key things in apps and programs generally.
开始研究 reg(ular) ex(presions) 是个好主意,因为它们在字符串格式化中很有用(想象一下,您必须测试输入是否是有效的电子邮件......)。还用于检查 SQL 注入以及应用程序和程序中的许多关键内容。