Reputation: 1230
I have to write a program which checks a password.
The password is valid
'The password is invalid
'. The program is working only for the if part but not for the else.
That is when I input bolt, it displays the correct message.
But when I enter something other than bolt, it does not display 'The password is invalid
'.
I have been told to test for all four characters that is to use char.At.
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your 4-character password:");
String password = input.next();
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) == 'B' || password.charAt(i) == 'b')
if (password.charAt(i + 1) == 'O'
|| password.charAt(i + 1) == 'o')
if (password.charAt(i + 2) == 'L'
|| password.charAt(i + 2) == 'l')
if (password.charAt(i + 3) == 'T'
|| password.charAt(i + 3) == 't') {
System.out.println("The password is valid");
}
else {
System.out.println("The password is invalid!");
}
}
}
Upvotes: 0
Views: 1344
Reputation: 87
What you are doing is not making much sense. You should probably use a character array an then check sequentially. Another small thing is that the else
block is associated with the last if
statement. Where it should be attached to the first if
. This is one place where you need to use braces intelligently.
Upvotes: 0
Reputation: 14471
For your updated question:
String password = input.next().toLowerCase();
String correctPassword = "bolt";
if (password.length() != correctPassword.length()) {
System.out.println("Not valid");
return;
}
for (int i = 0; i < correctPassword.length(); i++) {
if (password.charAt(i) != correctPassword.charAt(i)) {
System.out.println("Not valid");
return;
}
}
System.out.println("Valid");
In your code there are two problems,
1. Your for loop doesn't serve any purpose because you have checked the whole password in the first iteration itself. Your second iteration would cause IndexOutOfBoundsException
.
if
(which checks for the char "T" or "t").
Hope this helps..
Upvotes: 1
Reputation: 23535
Why don't you just check for String equality directly?
String password= input.next();
if("bolt".equals(password))) {
System.out.println("Valid password");
} else {
System.out.println("InValid password");
}
Or use equalsIgnoreCase()
if you'd also consider BOLT
or Bolt
valid.
If you need to implement this without equals
you could use something like this:
if (password != null &&
password.length() == 4 &&
(password.charAt(0) == 'B' || password.charAt(0) == 'b') &&
...) {
System.out.println("Valid password");
} else {
System.out.println("InValid password");
}
Upvotes: 2
Reputation: 1593
It would be more readable to check using equalsIgnoreCase()
:
String password= input.next();
if(password.equalsIgnoreCase("bolt"){
System.out.println("The password is valid");
}
else{
System.out.println("The password is invalid!");
}
Upvotes: 2
Reputation: 174716
I'did use string.matches
function which accepts regex as argument. (?i)
helps to do a case-insensitive match.
if(string.matches("(?i)bolt"))
{
System.out.println("Valid password");
}
else {System.out.println("InValid password");}
Upvotes: 1