Been fighting with this for awhile, still new to java and really struggling with this assignment. We're meant to make a password checker assignment that checks if 2 entered passwords are the same. Then also check them against some "requirements" such as password length of at least 8 characters, one special character, one upper and one lower case, no repeating characters more than 3 times consecutively, (ie "aaa").
Any input or criticism is very much appreciated and thank you in advance.
import java.util.Scanner;
/**
*
* #author Jonot
*/
public class Passwords {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//Asks and records users inputted passwords
Scanner input = new Scanner(System.in);
System.out.println("Please enter your password");
String password = input.nextLine();
System.out.println("Please re-enter your password");
String passwordCheck = input.nextLine();
//boolean to check if the inputted passwords meets requirements set below
boolean check;
check = isConfirmed(password);
while (!password.equals(passwordCheck) || (!check)) {
System.out.println("The password you entered is invalid");
System.out.println("Please try again");
password = input.nextLine();
System.out.println("please re-enter");
passwordCheck = input.nextLine();
check = isConfirmed(password);
//String password = input.nextLine();
//check = isConfirmed(password);
//if passwords meets boolean requirements,
if (isConfirmed(password)) {
//this will print out. Doesn't work right now.
System.out.println("password is valid");
} else {
}
}
}
//Boolean variables and they're set requirements. Do not work right now.
//Not sure why.
public static boolean isConfirmed(String password) {
Boolean leastOneUpperCase = false;
Boolean leastOneLowerCase = false;
Boolean leastOneDigit = false;
Boolean oneSpecialCharacter = false;
if (password.length() < 8) {
return false;
}
for (int i = 0; i < password.length(); i++) {
if (Character.isUpperCase(password.charAt {i
));
{
leastOneUpperCase = true;
}
else if (Character.isLowerCase(password.charAt(i)));
{
leastOneLowerCase = true;
}
else if (Character.isDigit(password.charAt(i)));
{
leastOneDigit = true;
}
}
return (leastOneUpperCase && leastOneLowerCase && leastOneDigit);
}
return false;
}
There was one primary problem, in the isConfirmed method. The return statement with the variables in it was inside the loop that checked each character. So, the return statement was executed after the first character was checked and therefore always returned false, since two out of the three booleans would still be false.
Also, looked like some semicolons were out of place in your charAt checks. Not sure if that's a formatting error introduced during your post. Finally, I thought the code would be more readable by pulling the current character being checked into a variable and using that instead of calling charAt a bunch of times.
Here's the revised code:
import java.util.Scanner;
/**
*
* #author Jonot
*/
public class Passwords {
/**
* #param args the command line arguments
*/
public static final void main(String[] args) {
//Asks and records users inputted passwords
Scanner input = new Scanner(System.in);
System.out.println("Please enter your password");
String password = input.nextLine();
System.out.println("Please re-enter your password");
String passwordCheck = input.nextLine();
//boolean to check if the inputted passwords meets requirements set below
boolean check;
check = isConfirmed(password);
while (!password.equals(passwordCheck) || (!check)) {
System.out.println("The password you entered is invalid");
System.out.println("Please try again");
password = input.nextLine();
System.out.println("please re-enter");
passwordCheck = input.nextLine();
check = isConfirmed(password);
//String password = input.nextLine();
//check = isConfirmed(password);
//if passwords meets boolean requirements,
if (isConfirmed(password)) {
//this will print out. Doesn't work right now.
System.out.println("password is valid");
} else {
}
}
}
//Boolean variables and they're set requirements. Do not work right now.
//Not sure why.
public static boolean isConfirmed(String password) {
Boolean leastOneUpperCase = false;
Boolean leastOneLowerCase = false;
Boolean leastOneDigit = false;
Boolean oneSpecialCharacter = false;
if (password.length() < 8) {
return false;
}
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (Character.isUpperCase(c))
{
leastOneUpperCase = true;
}
else if (Character.isLowerCase(c))
{
leastOneLowerCase = true;
}
else if (Character.isDigit(c))
{
leastOneDigit = true;
}
System.out.println("password is valid");
} else {
}
}
}
//Boolean variables and they're set requirements. Do not work right now.
//Not sure why.
public static boolean isConfirmed(String password) {
Boolean leastOneUpperCase = false;
Boolean leastOneLowerCase = false;
Boolean leastOneDigit = false;
Boolean oneSpecialCharacter = false;
if (password.length() < 8) {
return false;
}
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (Character.isUpperCase(c))
{
leastOneUpperCase = true;
}
else if (Character.isLowerCase(c))
{
leastOneLowerCase = true;
}
else if (Character.isDigit(c))
{
leastOneDigit = true;
}
}
return (leastOneUpperCase && leastOneLowerCase && leastOneDigit);
}
}
Related
I'm trying to figure out this problem. The directions are set hasDigit to true when a three character passCode from a scanner contains a digit.
Code below
import java.util.Scanner;
public class CheckingPasscodes {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
boolean hasDigit;
String passCode;
hasDigit = false;
passCode = scnr.next();
hasDigit = Character.isDigit(passCode);
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
}
}
I've entered in the line
hasDigit = Character.isDigit(passCode);
My logic is that character.isDigit is checking the passCode from the scanner, but I keep getting an error.
I've also tried:
hasDigit = Character.isDigit(0);
and the first test passes, but not the other. From this I assumed that I would have to enter in the string so it would test for anything, not just the character at position 0.
Thanks for the help.
import java.util.Scanner;
public class CheckingPasscodes {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
boolean hasDigit;
String passCode;
hasDigit = false;
passCode = scnr.next();
if ((Character.isDigit(passCode.charAt(0))) || (Character.isDigit(passCode.charAt(1))) || (Character.isDigit(passCode.charAt(2)))) {
hasDigit = true;
}
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
}
}
for(int i = 0 ;i <passCode.length();i++) {
hasDigit = Character.isDigit(passCode.charAt(i));
if (hasDigit) {
System.out.println("Has a digit.");
break;
}
}
if(!hasDigit) {
System.out.println("Has no digit.");
}
Pity that the answer was revealed but if you are going to spoil the learning process at least try giving the right code :/
isDigit() function of Character class excepts either one character or an integer. It does not excepts string , that's why you are getting error. You can modify your code to -
hasDigit = false;
passCode = scnr.next();
for(int i = 0 ;i <passCode.length();i++) {
hasDigit = Character.isDigit(passCode.charAt(i));
if(hasDigit)
break;
}
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
this will give you required result.
I want to write a program that checks the inserted password for:
Length is minimum of 8
At least 1 uppercase letter
At least 1 lowercase letter
At least 3 digits
I wrote this program, but it doesn't give me the right output:
import java.util.Scanner;
public class Question5 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Please enter your password: ");
String input = in.nextLine();
boolean flag = validate(input);
if (flag = true) {
System.out.println("password verified");
}
else {
System.out.println("not a good password");
}
}
public static boolean validate(String input) {
boolean flag = false;
int uppercaseCounter = 0;
int lowercaseCounter = 0;
int digitCounter = 0;
int letterCounter = 0;
for (int i = 0; i<(input.length()); i++) {
int totalCounter = digitCounter + letterCounter;
if (totalCounter >= 8 && digitCounter >= 3 && uppercaseCounter > 0 && lowercaseCounter > 0) {
flag = true;
}
else {
if (Character.isDigit(i)) {
digitCounter++;
}
if (Character.isLetter(i)) {
letterCounter++;
}
if (Character.isUpperCase(i)) {
uppercaseCounter++;
}
if (Character.isLowerCase(i)) {
lowercaseCounter++;
}
}
}
return flag;
}
}
Can someone help me with this? Thank you very much!
Here is the catch:
if (flag = true)
{
System.out.println("password verified");
}
= is an assignment operator == is the relational operator. To fix, do flag==true.
Also, in your method, you are comparing i, which is the counter, and not the
char At i. So do this
if(Character.isDigit(input.charAt(i))){ //Do this for all Character.isSomething() Methods
for all the checks you make.
You are actually checking the i counter in your various if instead of the input string...
use something like
char c = s.charAt(i);
and check the input chars
moreover you should change the check if(flag = true) with if(flag)
Change
if (flag = true)
{
System.out.println("password verified");
}
else
{
System.out.println("not a good password");
}
to
if (flag)
{
System.out.println("password verified");
}
else
{
System.out.println("not a good password");
}
When you write if(flag=true) then you are doing an assignment operation and not an equality comparison.
Also, the logic should be Character.isDigit(input.charAt(i)) since you want to check the character at i and not i itself.
To conclude, I would like to say that this problem would be fun to solve with Regular Expressions. Check this tutorial on regular expressions in Java.
In addition to other answers, this code should be moved below the counter increments:
int totalCounter = digitCounter + letterCounter;
if (totalCounter >= 8 && digitCounter >= 3 && uppercaseCounter > 0 && lowercaseCounter > 0) {
flag = true;
}
Otherwise, you run the risk of returning false when your password would become valid on the last character.
I've have this assignment where I need to make a login system in Java (I'm using Eclipse as IDE).
My login system works fine, but I am missing a loop- from where it is possible for the user to "try again". I would really like if the users could have 3 tries.
Is it possible, and can anybody help me?
This is my code:
package BookingSystemPackage;
import java.util.Scanner;
public class Booking_login {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your Social Security number in the format DDMMYY-XXXX: ");
String cpr;
cpr = input.next();
if (cpr.length() != 11 || cpr.charAt(6) != '-') {
System.out.println(cpr + " You have entered a invalid social security number");
}
else if (Character.isDigit(cpr.charAt(0))
== false || Character.isDigit(cpr.charAt(1))
== false || Character.isDigit(cpr.charAt(2))
== false || Character.isDigit(cpr.charAt(3))
== false || Character.isDigit(cpr.charAt(4))
== false || Character.isDigit(cpr.charAt(5))
== false || Character.isDigit(cpr.charAt(7))
== false || Character.isDigit(cpr.charAt(8))
== false || Character.isDigit(cpr.charAt(9))
== false || Character.isDigit(cpr.charAt(10))
== false) {
System.out.println("You have entered a invalid social security number");
}
else {
System.out.println("You have entered a valid social security number");
// ----------------------------------------------------------------------------------------
Scanner inputLogin = new Scanner(System.in);
System.out.println("Please write your user name: ");
String UserName = inputLogin.nextLine();
System.out.println("Please write your password: ");
String Password = inputLogin.next();
System.out.println((validateUser(UserName,Password)) ? "You are logged in" : "You are not logged in"
}
}
public static boolean validateUser(String UserName, String Password) {
String[] userNames = {"Super user", "User", "Admin", "Lucille"};
String[] passwords = {"AA11" , "BB22" , "CC33", "HVORFOR!"};
boolean chek = false;
for (int i = 0; i < userNames.length; i++) {
if (UserName.equals(userNames[i])) {
if (Password.equals(passwords [i])) { chek = true; }
}
}
return chek;
}
I would break the password checking into a separate method, that returns true or false depending on whether the user has succeeded in logging in. Then call that method and take the appropriate action depending on the return. So your main method might have something like
String password = "s3cr3t";
Scanner input = new Scanner(System.in);
boolean authenticated = login(password, 3, input);
if (authenticated) {
// Do some stuff here
} else {
System.out.println("Sorry, I can't let you do that");
}
and your login method might look like this.
public static boolean login(String password, int numberOfAttempts, Scanner input) {
for (int attempt = 1; attempt <= numberOfAttempts; attempt++) {
System.out.format("Attempt %d - Please type your password: ", attempt);
String passwordEntered = input.nextLine();
if (passwordEntered.equals(password)) {
return true;
}
}
return false;
}
This should give you an idea:
int i = 0;
boolean isLoggedIn = false;
String password = "123";
while (!isLoggedIn) {
System.out.print("Please enter your password: ");
String input = new Scanner(System.in).next();
if (input.equals(password)) {
System.out.println("Logged in!");
isLoggedIn = true;
} else {
if (i == 2) {
System.out.println("You have tried too many times. System will now shut down.");
System.exit(0);
}
System.out.println("Not logged in");
i += 1;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Any time I enter a password whether it is in valid format or invalid format, it always outputs "Invalid password".
import java.util.Scanner;
public class PasswordTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
boolean length = true;
boolean digit = true;
boolean lowercase = true;
boolean uppercase = true;
char ch = 0;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
if (s1.length()>=8){
length = true;
}
}
if (digit==false && lowercase==false && uppercase==false && length==false)
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
}
You need to first set all the boolean values to false at the time of declaration, and then in the code below set it to true only if it satisfies the condition. Also while printing "Valid Password" check if all boolean values are true, else print "Invalid Password". Checking if string length is greater than 8 should be outside the loop body. Following code works.
import java.util.Scanner;
public class PasswordTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
boolean length = false;
boolean digit = false;
boolean lowercase = false;
boolean uppercase = false;
char ch=0;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
}
if (s1.length()>=8){
length = true;
}
if (digit==true && lowercase==true && uppercase==true && length==true)
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
}
What you did is first set the boolean value to true then again in loop you are setting it to true which doesnot makes sense.I think this code will work fine for you
Scanner input = new Scanner(System.in);
boolean length = false;
boolean digit = false;
boolean lowercase = false;
boolean uppercase = false;
char ch = '\u0000';
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
if (s1.length()>=8){
length = true;
}
}
if (digit==true && lowercase==true && uppercase==true && length==true)
System.out.println("Valid password");
else
System.out.println("Invalid password");
For the future, I think regular expressions would be helpful.
Your password requirements seem to be "8 or more characters in length, where each character may be a lowercase letter, a capital letter, or a digit," is that right? We can express those requirements with a regular expression, such as ([a-z]|[A-Z]|[0-9]){8,}. With this, we may write
String password = input.nextLine();
if(password.matches("([a-z]|[A-Z]|[0-9]){8,}") {
// Go about your business.
}
I have a question for you, if it can't it be lowercase and if it cant be uppercase then what could it be? I made it so it has to be lowercase, here is the code (if you want it to be all uppercase then change this if statement in the code if(digit[i] == true || lowercase[i] == false || uppercase[i] == true) to if(digit[i] == true || lowercase[i] == true || uppercase[i] == false)
import java.util.Scanner;
public class PasswordTest{
public static void main (String[] args){
Scanner input = new Scanner(System.in);
boolean length;
boolean pass = true;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
boolean[] digit = new boolean[s1.length()];
boolean[] lowercase = new boolean[s1.length()];
boolean[] uppercase = new boolean[s1.length()];
char[] ch = new char[s1.length()];
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch[i] = s1.charAt(i);
digit[i] = Character.isDigit(ch[i]);
lowercase[i] = Character.isLowerCase(ch[i]);
uppercase[i] = Character.isUpperCase(ch[i]);
}
length = s1.length()>=8;
if (length==false && s1.length() > 0) {
int i;
for(i = 0; i < s1.length(); i ++){
if(digit[i] == true|| lowercase[i] == false|| uppercase[i] == true)
pass = false;
}
if(pass == true)
System.out.println("Valid password");
else{
System.out.println("Invalid password");
}
}else{
System.out.println("Invalid password");
}
}
}
I am not asking anyone to do my work I just need a little help solving this mismatch. This is my program:
import java.util.InputMismatchException;
import java.util.Scanner;
class FibonacciNumbers {
FibonacciNumbers() //default constructor
{
}
Scanner in = new Scanner(System.in);
public int fOf(int n)
{
if (n == 0) //the base case
{
return 0;
}
else if (n==1)
{
return 1;
}
else
{
return fOf(n-1)+fOf(n-2);
}
}
public static void main(String[] args)
{
FibonacciNumbers fNumbers = new FibonacciNumbers(); //creates new object
Scanner in = new Scanner(System.in);
String userInput;
int n = 0;
boolean IsRepeat = true ;
boolean isQuit;
boolean checkException = false;
isQuit = false;
while (!isQuit)
{
try {
{
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
n = in.nextInt();
System.out.print("The Fibanocci number for "+n+" is: ");
n = fNumbers.fOf(n);
System.out.println(n);
System.out.print("Do you want to run again? Press 'N' for No or anything else to continue: ");
userInput = in.next();
if(userInput.equalsIgnoreCase("N") )
{
isQuit = true;
System.out.println("Good-bye!");
}
else
{
IsRepeat = true;
}
}
}
catch(InputMismatchException ex) {
userInput = in.nextLine();
if ((userInput.charAt(0) == 'q') || (userInput.charAt(0) == 'Q') )
{
isQuit = true;
System.out.println("Good-bye!");
}
else {
checkException = true;
IsRepeat = true;
System.out.println("Invalid entry, Try again!");
}
}
catch (ArrayIndexOutOfBoundsException a)
{
n = in.nextInt();
if (n<0 || n>46)
{
System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
checkException = false;//sets boolean value to false, continues the loop
}
else
{
IsRepeat = true;
}
}
}
}
}
I did everything I got everything to work but at this part it is not going as I want it to run:
catch (ArrayIndexOutOfBoundsException a)
{
n = in.nextInt();
if (n<0 || n>46)
{
System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
checkException = false;//sets boolean value to false, continues the loop
}
else
{
IsRepeat = true;
}
}
When I run it if the user inputs higher than 46 or lower than 0 then ask them for a different input but it is just doing the math. It wont do as i wrote the program.
It throws a "java.lang.StackOverflowError" instead of an "ArrayIndexOutOfBoundsException".
The better way would be to catch an invalid input at
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
n = in.nextInt();
you could set the "n = in.nextInt();" into a do - while- loop,
like:
do {
ask for number
} while (check if number is correct);