Java 字符串长度、大写字母数和字符串中的数字数
我想检查Java 字符串长度、大写字母数和字符串中的数字数,java,string,charat,Java,String,Charat,我想检查字符串是否有8个或更多字符,以及是否有1个大写字母和1个数字 这是我的代码: import java.util.Scanner; public class PasswordTest { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); System.out.printf("Enter a password to be checked: \n");
字符串是否有8个或更多字符,以及是否有1个大写字母和1个数字
这是我的代码:
import java.util.Scanner;
public class PasswordTest
{
public static void main(String[] args)
{
Scanner keyb = new Scanner(System.in);
System.out.printf("Enter a password to be checked: \n");
String passwordInput = keyb.next();
int numberCharaters = passwordInput.length();
int numberCount = 1;
for (int i = 1; i <= numberCharaters; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
int numberNumbers = numberCount - 1;
int captialCount = 1;
for (int i = 1; i <= numberCharaters; i++)
{
for(char c = 'A'; c <= 'Z'; c++)
{
if (passwordInput.charAt(i) == c)
{
captialCount++;
}
}
}
int numberCaptials = captialCount - 1;
if (numberCharaters >= 8 && numberNumbers >= 1 && numberCaptials >= 1)
{
String strongEnough = "Password is strong enough.";
System.out.println(strongEnough);
}
else
{
String strongEnough = "Password is not strong enough.";
System.out.println(strongEnough);
}
}
}
我的输入是:Test1
我做错了什么?我一直在试图找出java.lang.StringIndexOutOfBoundsException:
的来源 您的程序有一些错误
您应该将numberCount
初始化为0
,以避免以后必须进行减法运算,并避免创建另一个变量。您得到的错误也是因为您犯了一个小错误
长度=元素(字符)的实际数量
索引=从0
开始,在length-1
结束
您的代码应如下所示:
import java.util.Scanner;
public class PasswordTest
{
public static void main(String[] args)
{
Scanner keyb = new Scanner(System.in);
System.out.printf("Enter a password to be checked: \n");
String passwordInput = keyb.next();
int numberCharaters = passwordInput.length();
int numberCount = 0;
for (int i = 0; i <= numberCharaters-1; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
int numberNumbers = numberCount - 0;
int captialCount = 0;
for (int i = 1; i <= numberCharaters; i++)
{
for(char c = 'A'; c <= 'Z'; c++)
{
if (passwordInput.charAt(i) == c)
{
captialCount++;
}
}
}
int numberCaptials = captialCount - 0;
if (numberCharaters >= 8 && numberNumbers >= 1 &&
numberCaptials >= 1)
{
String strongEnough = "Password is strong enough.";
System.out.println(strongEnough);
}
else
{
String strongEnough = "Password is not strong enough.";
System.out.println(strongEnough);
}
}
}
import java.util.Scanner;
公共类密码测试
{
公共静态void main(字符串[]args)
{
扫描仪keyb=新扫描仪(System.in);
System.out.printf(“输入要检查的密码:\n”);
字符串passwordInput=keyb.next();
int numbercharters=passwordInput.length();
整数计数=0;
对于(inti=0;iimportjava.util.*;
公共类密码
{
公共静态void main()
{
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“输入密码:”);
字符串s=sc.nextLine();
int l=s.长度();
int k=0,k1=0;
如果(l>=8)
{
对于(int i=0;i0&&k1>0)
{
System.out.println(“密码足够强”);
}
其他的
{
System.out.println(“密码应至少包含大写字母和一个数字”);
}
}
其他的
{
System.out.println(“密码应至少包含大写字母,一个数字,长度应为8或更多”);
}
}
}
索引从0
到numberofcharacters-1
,而不是1
到numberofcharacters
。请注意,您不需要内部循环,只需使用例如char c=passwordInput.charAt(i);if(c>=“0”和&cfor(int i=0;ifor(int i=0;i
@AndyTurner等待,那么JavaStuff nothing的numberofcharacters-1
@有什么问题吗?你认为为什么会有?
int numberCount = 0;
for (int i = 0; i < numberCharaters; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
import java.util.Scanner;
public class PasswordTest
{
public static void main(String[] args)
{
Scanner keyb = new Scanner(System.in);
System.out.printf("Enter a password to be checked: \n");
String passwordInput = keyb.next();
int numberCharaters = passwordInput.length();
int numberCount = 0;
for (int i = 0; i <= numberCharaters-1; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
int numberNumbers = numberCount - 0;
int captialCount = 0;
for (int i = 1; i <= numberCharaters; i++)
{
for(char c = 'A'; c <= 'Z'; c++)
{
if (passwordInput.charAt(i) == c)
{
captialCount++;
}
}
}
int numberCaptials = captialCount - 0;
if (numberCharaters >= 8 && numberNumbers >= 1 &&
numberCaptials >= 1)
{
String strongEnough = "Password is strong enough.";
System.out.println(strongEnough);
}
else
{
String strongEnough = "Password is not strong enough.";
System.out.println(strongEnough);
}
}
}
import java.util.*;
public class password
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the password : ");
String s=sc.nextLine();
int l=s.length();
int k=0,k1=0;
if(l>=8)
{
for(int i=0;i<l;i++)
{
if(Character.isLetter(s.charAt(i)))
{
if(Character.isUpperCase(s.charAt(i)))
{
k++;
}
}
else
{
if(Character.isDigit(s.charAt(i)))
{
k1++;
}
}
}
if(k>0&&k1>0)
{
System.out.println("Password is strong enough");
}
else
{
System.out.println("Password shoud contain atleast capital letter and one number");
}
}
else
{
System.out.println("Password shoud contain atleast capital letter,one number and shoud have length of 8 or more");
}
}
}