0
Hello 😇
What kinda method we use to get a char data from the user.. In Java lng. I mean, we use the next.Line() method to get a sequence of characters but i wanna know the one which w use to get a char.. (i used the next() but it doesn't work) Tnx in advance..
5 Antworten
+ 10
Scanner class doesn't have any method like nextChar()
Take the input as String using next() and consider the first character as the char input.
Scanner scan = new Scanner(System.in);
char c = scan.next().charAt(0);
Hope it helps :)
+ 8
charAt(int i) method returns the particular char at index i of the String. So you're taking the input as String but the first char is being extracted using this method. So if user enters "+", we're saving it as '+' which is a char.
It's possible to do operations this way based on input. See the example code, I've tried to write what you're aiming for :)
https://code.sololearn.com/cuF52SNcvX2q/#java
+ 5
There is another way for user input in Java known as BufferedReader. It has a function read() which is used for char input.
Learn more about BufferedReader here:
https://code.sololearn.com/cM5LBqctY4G8/?ref=app
+ 2
Tnx u guys..
I learned a lot from ur comments.
N ultimately, it's my simple calculator program in java 😂😇
https://code.sololearn.com/ch5T0B86cNdT/?ref=app
0
Tnx u Shamima
But I just wanted to get char as a userInput and use that as a operator.. Like addition or Multiplication operators..
In the way u mentioned the userInput get considered to be a string and instead of Calculating the operands together it causes them to get concatenated.. Like strings..
Aftrr getting the userInput im gonna switch on them n so on so far...
if u know the way of handling such kinda problems plz help me.. 😇