字符串长度、大写字母个数和字符串中数字的个数
string length, number of capitals, and number of numbers in a string
我想检查 string
是否有 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);
}
}
}
这是我收到的错误消息
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at passwordtest.main(passwordtest.java:23)
我的输入是:Test1
我做错了什么?我一直在试图找出 java.lang.StringIndexOutOfBoundsException:
的来源。
你的程序有一些错误。
您应该将 numberCount
初始化为 0
以避免以后必须减去,并避免必须创建另一个变量。你得到的错误也是因为 <=
,因为 no 字符串元素 = 到字符串的长度, 即 index 是从 0 到 length - 1.
int numberCount = 0;
for (int i = 0; i < numberCharaters; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
那么,对于capital count
你犯了同样的错误,我也会参考评论中的建议改进循环。
你犯了一个小错误。
长度 = 元素的实际数量(字符)。
索引 = 从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.*;
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");
}
}
}
我想检查 string
是否有 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);
}
}
}
这是我收到的错误消息
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at passwordtest.main(passwordtest.java:23)
我的输入是:Test1
我做错了什么?我一直在试图找出 java.lang.StringIndexOutOfBoundsException:
的来源。
你的程序有一些错误。
您应该将 numberCount
初始化为 0
以避免以后必须减去,并避免必须创建另一个变量。你得到的错误也是因为 <=
,因为 no 字符串元素 = 到字符串的长度, 即 index 是从 0 到 length - 1.
int numberCount = 0;
for (int i = 0; i < numberCharaters; i++)
{
for(char c = '0'; c <= '9'; c++)
{
if (passwordInput.charAt(i) == c)
{
numberCount++;
}
}
}
那么,对于capital count
你犯了同样的错误,我也会参考评论中的建议改进循环。
你犯了一个小错误。
长度 = 元素的实际数量(字符)。
索引 = 从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.*;
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");
}
}
}