Reputation: 23
I'm trying to make the input field turn into ***** when user enters their password. But here I'm getting 2 errors as shown below. How can i solve this. Could someone help modify this code so it works as it should. Thank you.
//package com.company;
import java.util.Scanner;
import java.io.Console;
public class Main {
static void myPassword(){
String Username = "wisdom";
String Password = "123";
boolean granted = false;
for(int i = 0; i < 3; i++) {
Scanner input1 = new Scanner(System.in);
Console console = System.console();
char[] password = console.readPassword("Enter password");
Arrays.fill(password, '*');
String username = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("Enter Password : ");
String password = input2.next();
if (username.equals(Username) && password.equals(Password)) {
granted = true;
break;
}
else if (username.equals(Username)) {
System.out.println("Invalid Password!");
} else if (password.equals(Password)) {
System.out.println("Invalid Username!");
} else {
System.out.println("Invalid Username & Password!");
}
}
if(granted)
System.out.println("Access Granted! Welcome!");
else
System.out.println("Access Denied! You have reached the maximum number of attempts");
System.exit(0);
}
public static void main(String[] args) {
myPassword();
}
}
Upvotes: 1
Views: 859
Reputation: 548
Maybe You can understand why occured errors by answers. Therefore I suggested my source code
import java.util.Scanner;
import java.io.Console;
public class Main {
static void myPassword(){
String username = "wisdom";
String password = "123";
boolean granted = false;
Console console = System.console();
Scanner scanner = new Scanner(System.in);
for(int i = 0; i < 3; i++) {
System.out.println("Enter username : ");
// i suggested variable should distinguished by name
String inputUsername = scanner.next();
char[] passwordChar = console.readPassword("Enter password");
// this need import java.util.Arrays;
// and if use this element of passwordChar[] changed *
//Arrays.fill(passwordChar, '*');
String inputPassword = new String(passwordChar);
if (username.equals(inputUsername) && password.equals(inputPassword)) {
granted = true;
break;
}
else if (username.equals(inputUsername)) {
System.out.println("Invalid Password!");
} else if (password.equals(inputPassword)) {
System.out.println("Invalid Username!");
} else {
System.out.println("Invalid Username & Password!");
}
}
//If you don't need to use scanner
//You had better close scanner
scanner.close();
if(granted)
System.out.println("Access Granted! Welcome!");
else
System.out.println("Access Denied! You have reached the maximum number of attempts");
System.exit(0);
}
public static void main(String[] args) {
myPassword();
}
}
Upvotes: 0
Reputation: 922
I think the errors are self explanatory here.
for first error : Arrays.fill(password, '*');
, the following import statement is missing.
import java.util.Arrays;
for second error: String password = input2.next();
, variable password is already defined at line char[] password = console.readPassword("Enter password");
. Simply a different variable name needs to be used here.
Upvotes: 1
Reputation: 352
First error: you need to import Arrays
before you can use it:
import java.util.Arrays;
Second error: you initialized a password
variable here already:
char[] password = console.readPassword("Enter password");
Choose another variable name instead for:
String password = input2.next();
Upvotes: 2