Re: Check Password Program
1: char != Character.. a quick look at the Character API and you would see that the isDigit and isLetterOrDigit would be statically called like or 2: Are you sure it is not valid if it contains a letter or digit?
3: Shouldn't the validity checks be in the loop?
4: Return false if any validity checks don't work... then return true at the end if you haven't returned false already
Re: Check Password Program
Thanks, I fixed up those parts of my code.
Here is the updated code:
Code :
import java.util.*;
import java.lang.String;
import java.lang.Character;
public class CheckingPassword {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Password: ");
String password = input.next();
if (isValid(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password");
}
}
public static boolean isValid(String password) {
if (password.length() < 8) {
return false;
} else {
char c;
for (int i = 0; i < password.length() - 1; i++) {
c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
return false;
} else {
return true;
}
}
}
return true;
}
}
How would I check to make sure the password contains at least 2 digits?
Re: Check Password Program
I would add in a counter and use the isDigit() method and if there is less than 2 digits, return false.
More than likely not the best solution.. I have 3 semesters of java over me in college so I'm still a beginner also!
Re: Check Password Program
That what the length() method of string is for, compare that to the minimum length and voila, you have that validity check.
Also, remember you return false if any characters are not right, but not if any character is correct. If nothing is wrong then you can return true. The part I am talking about is bolded below
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
return false;
} else {
return true;
}
Re: Check Password Program
Here's a more updated version of my code:
Code :
import java.util.*;
import java.lang.String;
import java.lang.Character;
public class CheckingPassword {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Password: ");
String password = input.next();
if (isValid(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password");
}
}
public static boolean isValid(String password) {
if (password.length() < 8) {
return false;
} else {
char c;
int count = 1;
for (int i = 0; i < password.length() - 1; i++) {
c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
count++;
if (count < 2) {
return false;
}
}
}
}
return true;
}
}
@ppme I added in a counter and it's still not working. I don't think I'm using the counter correctly. How would I fix it?
@Tjstretch I'm not sure I understand. I deleted the bolded part. Is that what I'm supposed to do?
Re: Check Password Program
This topic has just been cross posted here.
I got the code to work now.
Re: Check Password Program
Quote:
Originally Posted by
m2msucks
Here's a more updated version of my code:
Code :
import java.util.*;
import java.lang.String;
import java.lang.Character;
public class CheckingPassword {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Password: ");
String password = input.next();
if (isValid(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password");
}
}
public static boolean isValid(String password) {
if (password.length() < 8) {
return false;
} else {
char c;
int count = 0;
for (int i = 0; i < password.length() - 1; i++) {
c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
count++
}
}
if (count < 2 ) {
return false;
}
}
return true;
}
}
@ppme I added in a counter and it's still not working. I don't think I'm using the counter correctly. How would I fix it?
@Tjstretch I'm not sure I understand. I deleted the bolded part. Is that what I'm supposed to do?
Don't have the counter checker within the loop, because otherwise on the first char check, it checks the count <2 and finds it is false, then returns false, and the method terminates.
I see you have it done, but still if its something you didn't know, its good to know :). When you return true or false, at any point in a Boolean method the method terminates
Re: Check Password Program
I didn't know that before. Thanks.