Method to Check Password in Java Not Working
I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.
It needs to be at least eight characters long and can only have letters and digits.
In the grader, it passes every test except for passwordCheck("codingisawesome")
and passwordCheck("QWERTYUIOP")
.
Here's what I have so far:
public boolean passwordCheck(String password)
if (password.length() < 8)
return false;
else
char c;
int count = 0;
for (int i = 0; i < password.length(); i++)
c = password.charAt(i);
if (!Character.isLetterOrDigit(c))
return false;
else if (Character.isDigit(c))
count++;
if (count < 2)
return false;
return true;
If anyone can help, I'd appreciate it. Thanks.
java passwords
add a comment |
I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.
It needs to be at least eight characters long and can only have letters and digits.
In the grader, it passes every test except for passwordCheck("codingisawesome")
and passwordCheck("QWERTYUIOP")
.
Here's what I have so far:
public boolean passwordCheck(String password)
if (password.length() < 8)
return false;
else
char c;
int count = 0;
for (int i = 0; i < password.length(); i++)
c = password.charAt(i);
if (!Character.isLetterOrDigit(c))
return false;
else if (Character.isDigit(c))
count++;
if (count < 2)
return false;
return true;
If anyone can help, I'd appreciate it. Thanks.
java passwords
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39
add a comment |
I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.
It needs to be at least eight characters long and can only have letters and digits.
In the grader, it passes every test except for passwordCheck("codingisawesome")
and passwordCheck("QWERTYUIOP")
.
Here's what I have so far:
public boolean passwordCheck(String password)
if (password.length() < 8)
return false;
else
char c;
int count = 0;
for (int i = 0; i < password.length(); i++)
c = password.charAt(i);
if (!Character.isLetterOrDigit(c))
return false;
else if (Character.isDigit(c))
count++;
if (count < 2)
return false;
return true;
If anyone can help, I'd appreciate it. Thanks.
java passwords
I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.
It needs to be at least eight characters long and can only have letters and digits.
In the grader, it passes every test except for passwordCheck("codingisawesome")
and passwordCheck("QWERTYUIOP")
.
Here's what I have so far:
public boolean passwordCheck(String password)
if (password.length() < 8)
return false;
else
char c;
int count = 0;
for (int i = 0; i < password.length(); i++)
c = password.charAt(i);
if (!Character.isLetterOrDigit(c))
return false;
else if (Character.isDigit(c))
count++;
if (count < 2)
return false;
return true;
If anyone can help, I'd appreciate it. Thanks.
java passwords
java passwords
edited Nov 15 '18 at 4:23
Ketan Yekale
1,27421323
1,27421323
asked Nov 15 '18 at 3:19
Bob SmithBob Smith
12
12
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39
add a comment |
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39
add a comment |
3 Answers
3
active
oldest
votes
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password)
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;
return true;
return false;
add a comment |
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
add a comment |
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
return false;
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311915%2fmethod-to-check-password-in-java-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password)
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;
return true;
return false;
add a comment |
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password)
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;
return true;
return false;
add a comment |
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password)
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;
return true;
return false;
Assuming your requirement is as stated
It needs to be at least eight characters long and can only have letters and digits
Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,
public boolean passwordCheck(String password)
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;
return true;
return false;
answered Nov 15 '18 at 3:29
Elliott FrischElliott Frisch
155k1393187
155k1393187
add a comment |
add a comment |
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
add a comment |
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
add a comment |
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
Try an approach using patterns (this is simpler than looping):
public boolean passwordCheck(String password)
return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");
Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
edited Nov 15 '18 at 3:39
answered Nov 15 '18 at 3:31
FriwiFriwi
422212
422212
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
add a comment |
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
@Kartik True, I added it.
– Friwi
Nov 15 '18 at 3:39
add a comment |
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
return false;
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
add a comment |
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
return false;
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
add a comment |
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
return false;
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
It's failing those tests because your code checks that the password must have at least 2 digits:-
if (count < 2)
return false;
And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.
answered Nov 15 '18 at 3:42
KartikKartik
4,00231437
4,00231437
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311915%2fmethod-to-check-password-in-java-not-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?
– Elliott Frisch
Nov 15 '18 at 3:22
You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.
– Amadan
Nov 15 '18 at 3:22
from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that
– Kartik
Nov 15 '18 at 3:39