+ 1

I don't know what's wrong with my java code or what the error message means

The program is supposed to print out the password is valid if it's 8 or more characters and those characters must be letters or numbers and return false if it's less then 8 or has any special characters

20th Apr 2020, 8:43 PM
Zoe
Zoe - avatar
11 ответов
20th Apr 2020, 10:11 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Thinking you have not used the variable which you defined as character.
22nd Apr 2020, 12:44 PM
Anonymous
Anonymous - avatar
+ 1
I think you copied it from a website or somewhere where whitespace is used... Try typing code by yourself and this error will be removed...
20th Apr 2020, 9:14 PM
Arushi Singhania
Arushi Singhania - avatar
0
Plz post your code here too... It will help us in helping you..
20th Apr 2020, 8:57 PM
Arushi Singhania
Arushi Singhania - avatar
20th Apr 2020, 9:05 PM
Zoe
Zoe - avatar
0
Okay I'll try that
20th Apr 2020, 9:18 PM
Zoe
Zoe - avatar
0
https://code.sololearn.com/cj5Y6Gsov473/?ref=app It loads now, but doesn't function properly
20th Apr 2020, 9:22 PM
Zoe
Zoe - avatar
0
Zoe You can check the length outside the loop. < 8 -> return false Inside the loop check if indexof returns -1 -> return false If the pw passes the loop you can return true. public static boolean passwordCheck(String password) { String letters; letters= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; if(password.length() < 8) return false; for(int i =0; i<password.length();i++){ String character= password.substring(i,i+1); if(letters.indexOf(password.charAt(i)) == -1){ return false; } } return true; }
20th Apr 2020, 9:30 PM
Denise Roßberg
Denise Roßberg - avatar
0
"https://code.sololearn.com/cj5Y6Gsov473/?ref=app It loads now, but doesn't function properly” Hint: You declared the variable ‘character’, but you are not using it.
20th Apr 2020, 9:33 PM
Chris Persichetti
Chris Persichetti - avatar
0
With Denise Roßberg code it now calls passwords with special characters invalid but doesn't recognize password with less than 8 characters as false
20th Apr 2020, 9:46 PM
Zoe
Zoe - avatar
0
Zoe What do you mean? The method returns false if the pw length is < 8.
20th Apr 2020, 10:06 PM
Denise Roßberg
Denise Roßberg - avatar