Question

In: Computer Science

I don't understand why this method will not return true, even when my parameters for boolean...

I don't understand why this method will not return true, even when my parameters for boolean results are met, can anyone help shed some light on my mistake?

import java.util.*;
public class PasswordChecker{

   public static void main (String[]args){

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a password that is 8 characters in length.");
System.out.println("The password must have at least 3 uppercase letters,");
System.out.println("3 numeric digits, as well as 2 lowercase letters,");
System.out.println("and contain no special characters.");
System.out.println();
System.out.println("Enter a password:");

String password = scan.nextLine();
  
boolean result = checkPassword(password); // main method's call to the method named checkPassword

if(result){

   System.out.println(password + " is Valid.");}

else

   System.out.println(password + " is Invalid.");
   }

   public static boolean checkPassword(String password){
  
int digit = 0; int upper = 0; int lower = 0; int n = 0; int old = 0;
boolean digits = digit >=3;
boolean uppers = upper >=2;
boolean lowers = lower >=3;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers && only && goodLength;
  
while (true){
  
   for (n = 0; n < password.length(); n++) {

if (Character.isDigit(password.charAt(n))){
   digit++;}

if (Character.isLowerCase(password.charAt(n))){
   lower++;}

if (Character.isUpperCase(password.charAt(n))){
   upper++;}
  
if (!Character.isDigit(password.charAt(n)) && !Character.isLetter(password.charAt(n))){
   old++;}
   }
   System.out.println(upper);
   System.out.println(lower);
   System.out.println(digit);
   System.out.println(old);
  
   if (results){
return true;}
  
   if (!results){
return false;}
}
  
   }
}

Solutions

Expert Solution

There are two mistakes.

1) The following check should be done after for loop ends because only after for loop ends the variables digit,upper lower old will contain actual values computed from the string.

boolean digits = digit >=3;
boolean uppers = upper >=2;
boolean lowers = lower >=3;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers && only && goodLength;

2) In the password requirements the following is there but in checking upper and lower case checks are written wrongly, The password must have at least 3 uppercase letters,3 numeric digits, as well as 2 lowercase letters.

boolean uppers = upper >=2;
boolean lowers = lower >=3;

I have modified the code. Please find it below.

import java.util.*;
public class PasswordChecker{

public static void main (String[]args){

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a password that is 8 characters in length.");
System.out.println("The password must have at least 3 uppercase letters,");
System.out.println("3 numeric digits, as well as 2 lowercase letters,");
System.out.println("and contain no special characters.");
System.out.println();
System.out.println("Enter a password:");

String password = scan.nextLine();
  
boolean result = checkPassword(password); // main method's call to the method named checkPassword

if(result){

System.out.println(password + " is Valid.");}

else

System.out.println(password + " is Invalid.");
}

public static boolean checkPassword(String password){
  
int digit = 0; int upper = 0; int lower = 0; int n = 0; int old = 0;
  
while (true){
  
for (n = 0; n < password.length(); n++) {

if (Character.isDigit(password.charAt(n))){
digit++;}

if (Character.isLowerCase(password.charAt(n))){
lower++;}

if (Character.isUpperCase(password.charAt(n))){
upper++;}
  
if (!Character.isDigit(password.charAt(n)) && !Character.isLetter(password.charAt(n))){
old++;}
}
boolean digits = digit >=3;
boolean uppers = upper >=3;
boolean lowers = lower >=2;
boolean only = old < 1;
boolean goodLength = password.length() >= 8;
boolean results = digits && uppers && lowers && only && goodLength;
System.out.println(upper);
System.out.println(lower);
System.out.println(digit);
System.out.println(old);
  
if (results){
return true;}
  
if (!results){
return false;}
}
  
}
}

OUTPUT:

Please enter a password that is 8 characters in length.
The password must have at least 3 uppercase letters,
3 numeric digits, as well as 2 lowercase letters,
and contain no special characters.

Enter a password:
A1B2C3de
3
2
3
0
A1B2C3de is Valid.



Related Solutions

Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
I have the answers for these questions, according to my study guide. I don't understand how...
I have the answers for these questions, according to my study guide. I don't understand how the answers were obtained, though, so please show work! A) You are planning to take two exams. According to the records, the failure rates for the two exams are 15% and 25%, respectively. Additionally, 80% of the student who passed the exam 1 passed exam 2. (The 80% is based on the given condition.) What will be the probability that you fail the 1st...
I need to provide proof that these statements are FALSE. But I don't always understand why...
I need to provide proof that these statements are FALSE. But I don't always understand why they are false. 1. "drugs that block L-type Ca channels, reduce the contraction-efficiency of cardiac muscle, but not of skeletal muscles." (dont the skeletal muscles just need the tiniest bit of Ca to use the DHP channels?) 2. "the length of a skeletal muscle twitch is about 10-100 ms" (is this maybe for all muscle types? and not just for skeletal muscles?) thx in...
I have trouble when solving this problem. I don't understand "There are more than half of...
I have trouble when solving this problem. I don't understand "There are more than half of the Final scores " could refer to which? Please help me in solving this. Test the hypothesis that there are more than half of the Final scores that are 5 or below, at the significance level of 5%. Final Scores 9.2 4.8 6.6 5.8 3.4 5 5.8 3.4 2.2
please can you write the calc_plant method in another way, I don't understand way they assign...
please can you write the calc_plant method in another way, I don't understand way they assign these specific numbers to min, unhappy1...4. the question says if the plant is within two miles or less of a city, the unhappiness is infinite (that is, assign a very large number to the unhappiness for that city). 2) Otherwise, the unhappiness is equal to the population of the city divided by the distance of the plant from the city. The average unhappiness equals:...
I don't understand how the bonds chart work.
I don't understand how the bonds chart work.
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
I don't understand the zero chart when there is more than two numbers, i.e. z-3.17 &...
I don't understand the zero chart when there is more than two numbers, i.e. z-3.17 & z=2.79=?  
I have a homework class, but I don't really understand anything and I have to submit...
I have a homework class, but I don't really understand anything and I have to submit my homework next week. Homework must be written in C ++ program language. Can someone help me please... Working with classes (everything written below is one task): Define a class Date that contains integer variables for day, month, and year. 1.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments. 1.2. Create a method that calculates the number of...
I posted this question earlier but I don't quite understand the development for part b) and...
I posted this question earlier but I don't quite understand the development for part b) and c). I need some details because it's really not obvious for me. Part a) is good. The question is : We start the operation of a reversible Carnot Engine between two reservoir of temperature T1 and T2 where T2 > T1. The colder reservoir is so cold that his temperature doesn't change during the process of the engine. The hotter reservoir is composed of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT