0
Java Scanner
How to import a scanner that allows user to write characters not numbers?
8 odpowiedzi
+ 3
Ayman, you are trying to assign a string to a char variable. Try the following:
String password = "something";
Scanner program = new Scanner(System.in);
String yourPass = program.nextLine();
+ 2
Ayman you should compare strings using equals method.
if(yourpass.equals(password))
+ 1
Ayman
By using import Java.util.Scanner ;
We can still scan a character, can't we?
Scanner s =new Scanner (system.in);
Like char c=s.next().charAt(0);
Is this not what u are talking about?
+ 1
Netha_r2071,
Please review the given code example, it seems you forgotten something ...
+ 1
Scanner has a nextLine() method which returns a string, so it allows users to input characters. If you want to prohibit users from typing digits, it is not possible using Scanner, because digits are also characters. So you will need to come up with other solution. For example, parse user input using regex.
+ 1
It works now, thanks bro Aleksandrs Kalinins
0
package condition;
import java.util.Scanner;
public class control {
public static void main(String[] args) {
char password="something"; //The error is here
char yourpass;
Scanner program= new Scanner(System.in);
System.out.println("enter your password: ");
yourpass=program.next().charAt(0);
if (yourpass==password){
System.out.println("success!");
}
else {
System.out.println("your password is not correct!");
}
}
}
Netha_r2071
0
I did it,but still says the else content "your password is not correct" even if I enter the correct password Aleksandrs Kalinins