如何做java验证 [英] How to do java validation
问题描述
我在java中有1个标签,1个文本框用于用户输入,我希望用户类型与标签文本相同,如果他尝试键入任何一个错误的字符,系统应该说错字符。基本上它是一个输入验证。但是我无法使用输入字符来制作字符串。它应该逐字符比较,一旦用户键入正确的字符,系统应该回复确定。
喜欢这个=
Label =我的名字是
textbox =我的na(这里我应该输入m,但如果我输入b,它不应该输入,但是当我输入m时,它应该拍了。)
希望我能让你明白我想要的......请回复
我尝试过:
字符串测试= jLabel2.getText();
stringtoarray = test.toCharArray();
String test1 = jTextField2.getText();
char [] stringtoarray2 = test1.toCharArray();
for(int i = 0 ; i< test.length(); i ++)
{if(stringtoarray [i] == stringtoarray2 [i]){
System.out.println(相同);
}
其他
System.out.println(不同);
这个C#可以帮到你。
string main =1234;
string input =1244;
char [] stringtoarray2 = input.ToCharArray();
for(int i = 0; i< main.Length; i ++)
{
if(main [i] == stringtoarray2 [i])
{
Console.WriteLine(相同);
}
其他
{
Console.WriteLine(不同);
}
}
结果
相同
相同
不同
相同
为什么不使用 String.equals()
[ ^ ] ?
i have 1 label in java, 1 text box for user input, i want user type same as label text, if he try to type any one wrong character, system should say wrong character. basically its a input validation. but i am unable to comapare string with inputed character. it should compare character by character, as soon as user type correct character, system should reply ok.
like this=
Label= My name is
textbox= My na (here as i should type m, but if i typed b, it should not take input, but when i type m, it should taken.)
hope i make u understand what i want.. please respond
What I have tried:
String test= jLabel2.getText();
stringtoarray=test.toCharArray();
String test1=jTextField2.getText();
char [] stringtoarray2= test1.toCharArray();
for(int i = 0; i < test.length(); i++)
{if(stringtoarray[i]==stringtoarray2[i]){
System.out.println("same");
}
else
System.out.println("differ");
This's C# may help you.
string main = "1234"; string input = "1244"; char[] stringtoarray2 = input.ToCharArray(); for (int i = 0; i < main.Length; i++) { if (main[i] == stringtoarray2[i]) { Console.WriteLine("same"); } else { Console.WriteLine("differ"); } }
Result
same same differ same
Why not just use String.equals()
[^]?
这篇关于如何做java验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!