java问题,为什么index out of range: 6 ?
""Exceptioninthread"main"java.lang.StringIndexOutOfBoundsException:Stringindexoutofra...
""Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.charAt(String.java:658)
at PassordVerifier.main(PassordVerifier.java:15)
----jGRASP wedge2: exit code for process is 1."
import java.util.Scanner;
public class PassordVerifier
{
public static void main(String[] args)
{
String str;
Scanner k=new Scanner(System.in);
System.out.println("Enter your password,which should be at least six characters long,"+
"and which should be at least one uppercase,at least one lowercase character,and at least one digit.");
str=k.nextLine();
if(str.length()>=5)
{
for(int i=str.length();i>=0;i--){
if((!Character.isUpperCase(str.charAt(i)))&&(!Character.isLowerCase(str.charAt(i)))&&(!Character.isDigit(str.charAt(i))))
{
System.out.println("IT IS NOT VALID .");
}
}
System.out.println("OK");
}
else
{
System.out.println("IT IS NOT VALID.");
}
}
} 展开
at java.lang.String.charAt(String.java:658)
at PassordVerifier.main(PassordVerifier.java:15)
----jGRASP wedge2: exit code for process is 1."
import java.util.Scanner;
public class PassordVerifier
{
public static void main(String[] args)
{
String str;
Scanner k=new Scanner(System.in);
System.out.println("Enter your password,which should be at least six characters long,"+
"and which should be at least one uppercase,at least one lowercase character,and at least one digit.");
str=k.nextLine();
if(str.length()>=5)
{
for(int i=str.length();i>=0;i--){
if((!Character.isUpperCase(str.charAt(i)))&&(!Character.isLowerCase(str.charAt(i)))&&(!Character.isDigit(str.charAt(i))))
{
System.out.println("IT IS NOT VALID .");
}
}
System.out.println("OK");
}
else
{
System.out.println("IT IS NOT VALID.");
}
}
} 展开
5个回答
展开全部
java中String的下标是从0开始的,如果长度是6 那么下标的取值就是0-5 取不到6
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-09
展开全部
长度超过范围越界了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for(int i=str.length();i>=0;i--){//这一行
变为for(int i=str.length()-1;i>=0;i--){
变为for(int i=str.length()-1;i>=0;i--){
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第六行数组下标越界
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询