-2

I want to check if a string has 8 or more characters, and if it has 1 capital letter and 1 number.

This is my code:

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

and this is the error message I'm getting

  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)

My input was: Test1

What am I doing wrong? I've been trying to figure out where the java.lang.StringIndexOutOfBoundsException: comes from.

Nadim Baraky
  • 459
  • 5
  • 18

3 Answers3

0

Your program has some mistakes.

You should have initialized numberCount to 0 to avoid having to subtract later, and avoid having to create another variable. Also the error you're getting is because of the <=, since there is no string element = to the length of the string, i.e index is from 0 to 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++;
            }       
        }
    }

Then, for the capital count you made the same mistake, I would also take into account the advice in the comments to improve the loops.

Nadim Baraky
  • 459
  • 5
  • 18
Parco
  • 602
  • 5
  • 12
0

You are making a minor mistake.

Length = Actual number of elements(characters).

Index = starts from 0 and ends at length-1.

Your code should be look like this:

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

      }
   }
Nadim Baraky
  • 459
  • 5
  • 18
Najam
  • 1,129
  • 11
  • 32
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");
    }
  }
}
rayan
  • 1
  • 4