I successfully compile the code through cmd, however when i run it,
when i enter my password with anything(which fulfill the first condition (6-20))it will show error.
The question is to write a program that let user to create a password which is valid (more than 6 letters, must have atleast 1 uppercase,lowercase and digit)

Sorry if my post violent any rule(this is my first post !!)
p/s: i am quite new to java....

import java.util.Scanner;
public class Amazon	{
	public static void main(String[] args){
	Scanner input = new Scanner(System.in);
	User Account = new User();
	System.out.println("Account No: ");
	int userID = input.nextInt();
	System.out.println("Enter your desired password: ");
	String Password = Account.readPassword();
	}
}

import java.util.Scanner;
public class User	{
static Scanner input = new Scanner(System.in);
	String Password;
	User()	{
	}
	public String readPassword()	{
		String newPassword = input.nextLine();
		while (newPassword.length() < 6 | newPassword.length() > 20)	{
		System.out.println("Password length is between 6 - 20 characters.\nPassword: ");
		newPassword = input.nextLine();
		}
		boolean testLower = checkLower(newPassword);
		boolean testCapitalDigit = checkCapitalDigit(newPassword);
		while (testLower != true | testCapitalDigit != true )	{
		System.out.println("Invalid Password.Please enter a valid password.\nPassword: ");
		newPassword = input.nextLine();
		}
	return newPassword;
	}

	public boolean checkCapitalDigit(String newPassword)	{
		int Count = 0;
		for (int i = 0; i <= newPassword.length();i++)	{
			if(Character.isUpperCase(newPassword.charAt(i)) && Character.isDigit(newPassword.charAt(i)))	{
				Count++;
			}
		}
		boolean checkUpperDigit = (Count>0) ;
	return checkUpperDigit;
	}

	public boolean checkLower(String newPassword)	{
		int Count = 0;
		String lowerCase = newPassword.toLowerCase();
		for (int i = 0; i <= newPassword.length(); i++)	{
			if(lowerCase.charAt(i) == newPassword.charAt(i))	{
				Count++;
			}
		}
		boolean checkLower = (Count>0);
	return checkLower;
	}
}

Recommended Answers

All 6 Replies

You are getting an error because at line 36 and 48 you exit the bounds of your string.In Java an array starts at position 0 to array lenght-1 the string.lenght() returns the array size starting from position 1 thats why you are getting the error.You can easly solve this by replacing i <= newPassword.length() with

i < newPassword.lenght()

or

i <= newPassword.lenght()-1.

Thanks !
but now i got another problem(logic error), it seem that no matter what i enter, it shows invalid password all the time...

what error message do you get, and what does it depend on?

also, you may want to replace your | comparators by || comparators (basicly, they do the same work, but the second one will only check the second expression if it still needs to be checked)

I just tested: your problem is somewhere in your checkCapitalDigit method. try seperating the logic.
here's what you can do for upper cases:

String alphabet = "abcdefghijklmnopqrstuvwxyz";
        
        public boolean hasUpper(String password){
            String alph = alphabet.toUpperCase();
            char[] arr = password.toCharArray();
            for ( char cha : arr )
              if ( alph.indexOf(cha) != -1 )
                  return true;
            return false;
        }

with this, it should be very easy to find the right way to implement it, and write the right code to find whether it contains numbers or not.

Hmm, i wonder why it is still wrong....
i used the hasUpper as written here
it is always invalid password

I try to edit a few part yet it become as long as there is a digit and it is 6 characters then it will be valid...

import java.util.Scanner;
public class User	{
static Scanner input = new Scanner(System.in);
	String Password;
	User()	{
	}
	public String readPassword()	{
		String newPassword = input.nextLine();
		while (newPassword.length() < 6 | newPassword.length() > 20)	{
		System.out.println("Password length is between 6 - 20 characters.\nPassword: ");
		newPassword = input.next();
		}
		boolean testDigit = checkDigit(newPassword);
		boolean testUpper = checkCapital(newPassword);
		boolean testLower = checkLower(newPassword);
		while (testDigit != true | newPassword.length() < 6 | newPassword.length() > 20 | testUpper != true | testLower != true)	{
		System.out.println("Invalid Password.\nYour password should have atleast 1 lower case, 1 upper case and 1 digit");
		System.out.print("Password: ");
		newPassword=input.next();
		testDigit = checkDigit(newPassword);
		testUpper = checkCapital(newPassword);
		testLower = checkLower(newPassword);
		}
	return newPassword;
	}
	public boolean checkDigit(String newPassword)	{
		int Count = 0;
		for (int i = 0; i < newPassword.length();i++)	{
			if(Character.isDigit(newPassword.charAt(i)))	{
				Count++;
			}	
		}
		boolean checkDigit = (Count>0) ;
	return checkDigit;
	}
	public boolean checkLower(String newPassword)	{
		int Count = 0;
		String lowerCase = newPassword.toLowerCase();
		for (int i = 0; i < newPassword.length(); i++)	{
			if(lowerCase.charAt(i) == newPassword.charAt(i))	{
				Count++;
			}
		}
		boolean checkLower = (Count>0);
	return checkLower;
	}
	public boolean checkCapital(String newPassword)	{
		int Count = 0;
		String UpperCase = newPassword.toUpperCase();
		for (int i = 0; i < newPassword.length(); i++)	{
			if(UpperCase.charAt(i) == newPassword.charAt(i))	{
				Count++;
			}
		}
		boolean checkUpper = (Count>0);
	return checkUpper;
	}
}

Appearancely only the character.isDigit works
i tried with isLowerCase and isUpperCase, both work just fine

public boolean checkLower(String newPassword)	{
		int Count = 0;
		for (int i = 0; i < newPassword.length();i++)	{
			if(Character.isLowerCase(newPassword.charAt(i)))	{
				Count++;
			}	
		}
		boolean checkLower = (Count>0);
	return checkLower;
	}

	public boolean checkCapital(String newPassword)	{
		int Count = 0;
		for (int i = 0; i < newPassword.length();i++)	{
			if(Character.isUpperCase(newPassword.charAt(i)))	{
				Count++;
			}	
		}
		boolean checkUpper = (Count>0);
	return checkUpper;
	}

i used the hasUpper as written here

well .. according to the code you posted here .. no, you haven't. I showed you a way to write your own method that would detect if there's an upper case or not, and you can write (in the same way) a method that detects whether or not there's a digit in the String.

if you find a method that doesn't work the way you want it to, write a method that does :)
the code I provided you with, should give you enough to write the methods you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.