I am trying to create a java password code where the password is at least 6 characters Long, at least 1 UPPERCASE, & at least 1 lowercase & at least 1 Digit.Here is an example: Passw3

Can someone help me because it is not working. Here is the code

private static boolean isValid(String custNumber)
{
    boolean goodSoFar = true;
    int index = 0;

    // Test if string the correct length
    if (custNumber.length() !=6)
        goodSoFar = false;

    // Test if string has at least one UPPER case
    while (goodSoFar && index < 6)
    {
        if (!Character.isUpperCase(custNumber.charAt(index)))
        goodSoFar = false;
        index++;
    }

    //Test if string has at least one lower case

    while (goodSoFar && index < 6)
    {
        if (!Character.isLowerCase(custNumber.charAt(index)))
        goodSoFar = false;
        index++;
    }

    //Test if string has at least one Digit

    while (goodSoFar && index < 6)
    {
        if (!Character.isDigit(custNumber.charAt(index)))
        goodSoFar = false;
        index++;
    }

    return goodSoFar;

}
}
if (custNumber.length() !=6)

Doesn't take strings longer than 6 characters (which are allowed) into account.

Everywhere else, take the actual length instead of 6 as the limit, and keep in mind that the index starts at 0, not 1.

Then think about what you're doing. You reject the string as soon as any character doesn't match what you're checking.
That's of course incorrect.
You need to count the number of characters matching your requirement and reject only if that number is too low (or better accept as soon as it's high enough).

if (custNumber.length() !=6)

Doesn't take strings longer than 6 characters (which are allowed) into account.

Everywhere else, take the actual length instead of 6 as the limit, and keep in mind that the index starts at 0, not 1.

Then think about what you're doing. You reject the string as soon as any character doesn't match what you're checking.
That's of course incorrect.
You need to count the number of characters matching your requirement and reject only if that number is too low (or better accept as soon as it's high enough).

Ok I change the length if the character is less than 6 it should be false. Now Can you please help me with just the Uppercase code and I think I can do the rest

private static boolean isValid(String custNumber)
{
boolean goodSoFar = true;
int index = 0;

// Test if string the correct length
if (custNumber.length() < 6)
goodSoFar = false;

// Test if string has at least one UPPER case
while (goodSoFar && index < 6)
{
if (!Character.isUpperCase(custNumber.charAt(index)))
goodSoFar = false;
index++;
}

//Test if string has at least one lower case

while (goodSoFar && index < 6)
{
if (!Character.isLowerCase(custNumber.charAt(index)))
goodSoFar = false;
index++;
}

//Test if string has at least one Digit

while (goodSoFar && index < 6)
{
if (!Character.isDigit(custNumber.charAt(index)))
goodSoFar = false;
index++;
}

return goodSoFar;

}
}

I already told you what to do.
Count the number of uppercase characters you encounter and as soon as it reaches the minimum number required set the flag to true (start with the flag at false) and break out of the loop.

I already told you what to do.
Count the number of uppercase characters you encounter and as soon as it reaches the minimum number required set the flag to true (start with the flag at false) and break out of the loop.

Hi thank you for helping me but you have to understand me am a newbie in java. This is what I did and it is still not working

private static boolean isValid(String custNumber)
{
boolean goodSoFar = false;
int index = 0;
// Test if string the correct length
if (custNumber.length() < 6)
goodSoFar = false;
// Test if string has at least one UPPER case
while (goodSoFar && index >= 1)
{
if (!Character.isUpperCase(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
//Test if string has at least one lower case
while (goodSoFar && index >= 1)
{
if (!Character.isLowerCase(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
//Test if string has at least one Digit
while (goodSoFar && index >= 1)
{
if (!Character.isDigit(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
return goodSoFar;
}
}

You do have to set the flag to false before each step.
If it fails one test, skip the rest as the entire string is invalid.
If it passes one test, reset it to false and try the next test.
That way it will be true only if it passes all the tests.

This is what I have done so far

private static boolean isValid(String custNumber)
{
boolean goodSoFar = false;
int index = 0;
// Test if string the correct length
if (custNumber.length() >= 6)
goodSoFar = true;
// Test if string has at least one UPPER case
while (goodSoFar && index >= 1)
{
if (!Character.isUpperCase(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
//Test if string has at least one lower case
while (goodSoFar && index >= 1)
{
if (!Character.isLowerCase(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
//Test if string has at least one Digit
while (goodSoFar && index >= 1)
{
if (!Character.isDigit(custNumber.charAt(index)))
goodSoFar = true;
index++;
}
return goodSoFar;
}
}

You should change your while loop as follow: As well, you need to re-initialize your index to zero, each time you are verifying.

int index = 0;
while (goodSoFar && index < 6) {
     // your checking here ...
}

index = 0;
while (goodSoFar && index < 6) {
     // your checking here ...
}

I dont understand can you please it in the code above in one example for me to see it

private static boolean isValid(String custNumber) {
   boolean goodSoFar = false;
   int index = 0;

   // Test if string the correct length
   if (custNumber.length() >= 6) {
      goodSoFar = true;
   }

   // Test if string has at least one UPPER case
   while (goodSoFar && index < 6) {
      if (!Character.isUpperCase(custNumber.charAt(index))) {
         goodSoFar = true;
         index++;
      }
   }

   //Test if string has at least one lower case

   index = 0
   while (goodSoFar && index < 6) {
      if (!Character.isLowerCase(custNumber.charAt(index))) {
         goodSoFar = true;
         index++;
      }
   }

    // .. and so on
}

I have been on this the whole day but it is not working! I am trying to create a java password code where the password is at least 6 characters Long, at least 1 UPPERCASE, & at least 1 lowercase & at least 1 Digit.Here is an example: Passw3

private static boolean isValid(String custNumber) {
boolean goodSoFar = true;
int index = 0;
// Test if string the correct length
if (custNumber.length() < 6) {
goodSoFar = false;
}
// Test if string has at least one UPPER case
while (goodSoFar && index < 1) {
if (!Character.isUpperCase(custNumber.charAt(index))) {
goodSoFar = false;
index++;
}
}
//Test if string has at least one lower case
index = 0;
while (goodSoFar && index < 1) {
if (!Character.isLowerCase(custNumber.charAt(index))) {
goodSoFar = false;
index++;
}
}
//Test if string has at least one Digit
index = 0;
while (goodSoFar && index < 1) {
if (!Character.isDigit(custNumber.charAt(index))) {
goodSoFar = false;
index++;
}
}
return goodSoFar;
}
}

Try this. This should work 100%

private static boolean isValid(String custNumber) {
	boolean goodSoFar = true;
	boolean isOK = false;

	// Test if string the correct length
	if (custNumber.length() < 6) {
		goodSoFar = false;
	}

	if (goodSoFar) {
		// Test if string has at least one UPPER case		
		for (int i=0;i < custNumber.length() && !isOK;i++ ) {
			if (Character.isUpperCase(custNumber.charAt(i))) {
				isOK = true;
			}
		}

		if (!isOK)
			goodSoFar = false;
		isOK = false;
		for (int i=0;i < custNumber.length() && !isOK;i++ ) {
			if (Character.isLowerCase(custNumber.charAt(i))) {
				isOK = true;
			}
		}
		if (!isOK)
			goodSoFar = false;
		isOK = false;
		for (int i=0;i < custNumber.length() && !isOK;i++ ) {
			if (Character.isDigit(custNumber.charAt(i))) {
				isOK = true;
			}
		}
		if(!isOK)
			goodSoFar = false;
	}
	return goodSoFar;

}

It would be if you use one for-loop instead of 3. You could have 3 boolean variables inside the for-loop taking values true or false depending on the current character. And at the end check if all of them are true

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.