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..

12th Dec 2017, 6:53 AM
UbuUunTu✔️
UbuUunTu✔️ - avatar
6 odpowiedzi
+ 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 :)
12th Dec 2017, 6:55 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 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
12th Dec 2017, 7:49 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 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
12th Dec 2017, 11:56 AM
DAB
DAB - avatar
+ 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
12th Dec 2017, 10:14 AM
UbuUunTu✔️
UbuUunTu✔️ - avatar
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.. 😇
12th Dec 2017, 7:19 AM
UbuUunTu✔️
UbuUunTu✔️ - avatar