Java Scanner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java Scanner

How to import a scanner that allows user to write characters not numbers?

8th Sep 2020, 2:14 PM
Ayman
Ayman - avatar
8 Answers
+ 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();
8th Sep 2020, 8:13 PM
Aleksandrs
Aleksandrs - avatar
+ 2
Ayman you should compare strings using equals method. if(yourpass.equals(password))
8th Sep 2020, 9:04 PM
Aleksandrs
Aleksandrs - avatar
+ 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?
8th Sep 2020, 2:22 PM
RuntimeERROR
RuntimeERROR - avatar
+ 1
Netha_r2071, Please review the given code example, it seems you forgotten something ...
8th Sep 2020, 2:31 PM
Ipang
+ 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.
8th Sep 2020, 2:46 PM
Aleksandrs
Aleksandrs - avatar
+ 1
It works now, thanks bro Aleksandrs Kalinins
8th Sep 2020, 9:09 PM
Ayman
Ayman - avatar
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
8th Sep 2020, 3:02 PM
Ayman
Ayman - avatar
0
I did it,but still says the else content "your password is not correct" even if I enter the correct password Aleksandrs Kalinins
8th Sep 2020, 8:47 PM
Ayman
Ayman - avatar