0

How to improve this?

Is it possible to make this code more readable? Thanks for your help ;) https://code.sololearn.com/c3rasIza3vVj/#java

21st Mar 2020, 4:22 PM
Hélène
Hélène - avatar
5 Respuestas
+ 5
This is your if statement after removing unnecessary parentheses: if (password.length() >= 7 && password.matches("(.*[a-zA-Z]{1,}.*){2,}") && password.matches("(.*\\d{1,}.*){2,}") && password.matches("(.*[!@#$%&*]{1,}.*){2,}")) If you read the assignment again you might notice that your first regex is unnecessary. It seems that you don't need {1, } in your regex also. You could also use methods to explain better the purpose of the regex. E.g: if(password.length() >=7 && hasTwoOrMoreNumbers(password) && hasTwoOrMoreSpecialChars(password){ //Strong }
21st Mar 2020, 4:49 PM
Kevin ★
+ 2
Share what you did. It's ok if you are not familiar with methods. This is a beginner community. Let me see your methods code and i'll try to help.
31st Mar 2020, 10:27 PM
Kevin ★
+ 1
Thank you Kevin Star for your help. The code works with your advice and becomes more readable ! However, the code crashes when I try to define my own method. I'm not yet familiar with this.
31st Mar 2020, 7:55 PM
Hélène
Hélène - avatar
+ 1
Hello, thank you again for your help =) This is my new code with the modifications you advised me. Regex works, but I guess I misdefined the methods. I re-read the java course on the subject but I can't fix the problem. https://code.sololearn.com/cMr0k82Zo35c/#java
4th Apr 2020, 4:23 PM
Hélène
Hélène - avatar
+ 1
1- Put your methods inside the Program class but outside your main method. 2- Don't add a ; after the ( ) 3- These methods take a String argument and return a boolean value: static boolean hasTwoOrMoreSpecialChars(String str) { return str.matches("(.*[!@#$%&*].*){2,}"); } 4- You can use the names you want😅
5th Apr 2020, 12:51 AM
Kevin ★