字符串长度,大写字母数和字符串中的数字数

时间:2017-11-04 20:39:07

标签: java string charat

我想检查[)]是否有8个或更多字符,以及是否有1个大写字母和1个数字。

这是我的代码:

string

这是我得到的错误信息

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的来源。

3 个答案:

答案 0 :(得分:0)

你的程序有一些错误。

您应该已将numberCount初始化为0以避免以后必须减去,并避免必须创建另一个变量。您收到的错误也是由于<=,因为没有字符串元素 = 到字符串的长度,即 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,我还会考虑评论中的建议以改善循环。

答案 1 :(得分: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);
            }

      }
   }

答案 2 :(得分:0)

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");
    }
  }
}