Java IsleterOrdigit()方法,isDigit(),isLetter()方法
我试图找出如何检查Java IsleterOrdigit()方法,isDigit(),isLetter()方法,java,methods,Java,Methods,我试图找出如何检查字符串,以验证其中是否至少包含一个字母和一个数字。我会事先告诉你这是家庭作业,我有点困惑 有一种方法IsleterOrdigit()似乎是正确的方法,但我不确定如何在代码中实现它。下面是我正在使用的代码: import javax.swing.JOptionPane; public class Password { public static void main(String[] args) { String initialPassword;
字符串
,以验证其中是否至少包含一个字母和一个数字。我会事先告诉你这是家庭作业,我有点困惑
有一种方法IsleterOrdigit()
似乎是正确的方法,但我不确定如何在代码中实现它。下面是我正在使用的代码:
import javax.swing.JOptionPane;
public class Password
{
public static void main(String[] args)
{
String initialPassword;
String secondaryPassword;
int initialLength;
initialPassword = JOptionPane.showInputDialog(null, "Enter Your Passowrd.");
initialLength = initialPassword.length();
JOptionPane.showMessageDialog(null, "initialLength = " + initialLength);
while (initialLength < 6 || initialLength > 10)
{
initialPassword = JOptionPane.showInputDialog(null, "Your password does not meet the length requirements. It must be at least 6 characters long but no longer than 10.");
initialLength = initialPassword.length();
}
//Needs to contain at least one letter and one digit
secondaryPassword = JOptionPane.showInputDialog(null, "Please enter your password again to verify.");
JOptionPane.showMessageDialog(null, "Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword);
while (!secondaryPassword.equals(initialPassword))
{
secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again.");
}
JOptionPane.showMessageDialog(null, "The program has successfully completed.");
}
}
import javax.swing.JOptionPane;
公共类密码
{
公共静态void main(字符串[]args)
{
字符串初始化密码;
字符串二次密码;
int初始长度;
initialPassword=JOptionPane.showInputDialog(null,“输入您的密码”);
initialLength=initialPassword.length();
JOptionPane.showMessageDialog(null,“initialLength=“+initialLength”);
而(初始长度<6 | |初始长度>10)
{
initialPassword=JOptionPane.showInputDialog(null,“您的密码不符合长度要求。密码长度必须至少为6个字符,但不得超过10个。”);
initialLength=initialPassword.length();
}
//需要至少包含一个字母和一个数字
secondaryPassword=JOptionPane.showInputDialog(null,“请再次输入密码进行验证”);
JOptionPane.showMessageDialog(null,“初始密码:+initialPassword+”\n第二个密码:+secondaryPassword);
而(!secondaryPassword.equals(initialPassword))
{
secondaryPassword=JOptionPane.showInputDialog(null,“您的密码不匹配。请重新输入密码”);
}
showMessageDialog(null,“程序已成功完成”);
}
}
我想实现一个方法,其中comment部分使用isDigit()
,isleter()
,或isleterOrdigit()
方法,但我不知道如何做
任何指导都将不胜感激。提前感谢您的帮助。这应该行得通
public boolean containsBothNumbersAndLetters(String password) {
boolean digitFound = false;
boolean letterFound = false;
for (char ch : password.toCharArray()) {
if (Character.isDigit(ch)) {
digitFound = true;
}
if (Character.isLetter(ch)) {
letterFound = true;
}
if (digitFound && letterFound) {
// as soon as we got both a digit and a letter return true
return true;
}
}
// if not true after passing through the entire string, return false
return false;
}
如果不给你所有的代码就很难帮助你完成它,因为它很短 无论如何,首先,因为您至少需要一个字母和一个数字,所以您需要两个标志,两个
布尔值
,它们最初将是假
。通过使用foreach
循环,您可以在初始密码中迭代每个char
:
for (char c : initialPassword.toCharArray())
然后,您所要做的就是在每次迭代中检查c
是否可能是一个字母或数字,如果是,则设置相应的标志。循环终止后,如果设置了两个标志,则密码有效。这就是您的代码的外观:
boolean bHasLetter = false, bHasDigit = false;
for (char c : initialPassword.toCharArray()) {
if (Character.isLetter(c))
bHasLetter = true;
else if (Character.isDigit(c))
bHasDigit = true;
if (bHasLetter && bHasDigit) break; // no point continuing if both found
}
if (bHasLetter && bHasDigit) { /* valid */ }
下面的代码是我根据您的建议得出的最终代码:
import java.util.Scanner;
public class Password
{
public static void main(String[] args)
{
String initialPassword;
String secondaryPassword;
int numLetterCheck = 0;
int initialLength;
boolean digitFound = false; boolean letterFound = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a new password: ");
initialPassword = keyboard.nextLine();
initialLength = initialPassword.length();
System.out.println("Your initial password length is: " + initialLength);
while (initialLength < 6 || initialLength > 10)
{
System.out.println("Your password does not meet the length requirements of >6 and <10. Please enter a new password.");
initialPassword = keyboard.nextLine();
initialLength = initialPassword.length();
}
for (char ch : initialPassword.toCharArray())
{
if (Character.isDigit(ch))
{
digitFound = true;
}
if (Character.isLetter(ch))
{
letterFound = true;
}
if (digitFound && letterFound)
{
numLetterCheck = 0;
}
else
{
numLetterCheck = 1;
}
}
while (numLetterCheck == 1)
{
System.out.println("Your password must contain at least one number and one number. Please enter a new passord that meets this criteria: ");
initialPassword = keyboard.nextLine();
for (char ch : initialPassword.toCharArray())
{
if (Character.isDigit(ch))
{
digitFound = true;
}
if (Character.isLetter(ch))
{
letterFound = true;
}
if (digitFound && letterFound)
{
numLetterCheck = 0;
}
else
{
numLetterCheck = 1;
}
}
}
System.out.println("Please enter your password again to verify it's accuracy; ");
secondaryPassword = keyboard.nextLine();
System.out.println("Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword);
while (!secondaryPassword.equals(initialPassword))
{
System.out.println("Your passwords do not match. Please enter your password again to verify.");
secondaryPassword = keyboard.nextLine();
}
System.out.println("The program has successfully completed.");
}
import java.util.Scanner;
公共类密码
{
公共静态void main(字符串[]args)
{
字符串初始化密码;
字符串二次密码;
int numLetterCheck=0;
int初始长度;
boolean digitFound=false;boolean letterFound=false;
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入新密码:”);
initialPassword=keyboard.nextLine();
initialLength=initialPassword.length();
System.out.println(“您的初始密码长度为:“+initialLength”);
而(初始长度<6 | |初始长度>10)
{
System.out.println(“您的密码不符合>6和的长度要求这似乎是一个老问题,在前面已经回答过,但我正在添加我的代码,因为我遇到了一个泰国口音字符的问题。因此,我致力于解决该问题,并找到了上述解决方案,如果您正在处理此类字符,则该解决方案是不完整的-ก่อนที่สุด ท้ายo
为了正确识别这些字符,代码如下:
String value = "abc123ก่อนที่สุด ท้ายo";
// Loop through characters in this String.
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
// See if the character is a letter or not.
if (Character.isLetter(c)) {
System.out.println("This " + c + " = LETTER");
}
if (Character.isDigit(c)) {
System.out.println("This " + c + " DIGIT");
}
if ((""+c).matches("\\p{M}"))
System.out.println("This " + c + " = UNICODE LETTER");
}
String value=“abc123ก่อนที่สุด ท้ายo”;
//循环遍历此字符串中的字符。
对于(int i=0;i
不确定是否有人也遇到过这种情况。希望这能有所帮助。您可以使用以下代码:
import java.util.Scanner;
公共类IsDigitisLetter{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
字符;
System.out.println(“请输入单个字符:”);
character=scnr.next().charAt(0);
System.out.println(字符);
if(字符。Isleter(字符)){
System.out.println(“输入的字符是字母”);
}else if(Character.isDigit(Character)){
System.out.println(“输入的字符是数字。”);
}
}
}
这并没有回答原来的问题。