Ask coding questions

← Back to all posts
what method do I use to get a single char to complete the statement.
GregBeutler


Is there one?, in C it was _getchar, if I recall.
I just want to type in 'w' or similar and get the code to process that statement and do the next thing
i.e. the 'w' entered into the user's keyboard should be enough for an event handler to be triggered to give me the 'char' entered.

see my simple repl.it above.

Voters
NARUTO12456789234567
SuperUser111
intriguing-
GregBeutler
Comments
hotnewtop
NARUTO12456789234567

why is the system.in.read

DorshReal

There are some problems in this code:

  • You are using the hasNext() method, which is more favourable in a recursive nature if you want to continue receiving input, suitable for a while loop instead of an if statement
  • You are using the read() function in conjunction with System.in along with the wrong input class (Scanner). Use a buffered reader instead to access read() method, or just replace it with in.nextChar()
  • Why are you using a char to hold a string input? A name contains more than one character, and last time I checked char variables can only hold one character. Replace this with a string variable for reading input
  • Lastly, what event handler are you trying to use? This program only facilitates input/output operations. If you want to trigger events with the keyboard, import the key listener class and implement the methods necessary.
SuperUser111

What are you trying to do?
If you simply want to print out what they entered, use this code (it's in java):

try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){ final String name = br.readLine(); System.out.println("Your name is: "+name); }

With some formatting that should work. If that wasn't what you were looking for then could you explain it to me? Thanks

GregBeutler

@SuperUser111
so very close the problem
is the "readLine()"
I only have 1 char coming in from an arduino, which I don't have source code to and it gives me a 'w' or a 'g'
and I want to have my java program wait for the letter
which this program you shared does.
but NOThing progresses, until the <enter> key is pressed
because I'm suspecting your
br.readline()

if I remove the reaLine()

i see in the console
Your name is: [email protected]

how do I just get the char typed in??

SuperUser111

@GregBeutler Do you know Python?

This program (https://repl.it/@eekboi/Spell-Checker#main.py) seems to handle the pressed key well. I'm not too proficient in Python, but hopefully you can find someway to utilize his program to your needs.

If you want to copy the code over, use the 'fork' button.

I honestly don't know how to create a program for this situation :( If not, is there anyway you can make the Aduino press the <enter> key?

GregBeutler

@SuperUser111
thanks , that's exactly what I wanted.
I'll take the python for now.
I was hoping for java version as well.

DynamicSquid
GregBeutler

@DynamicSquid
-- sort of works. the problem, is that for it to function it requires me to hit the <enter> key. I just want a single key
to start the process and let it complete.

Aqtion

Um, is your issue resolved because I don't fully understand what you are trying to say.

GregBeutler

@kbadrinath_tcsp

i want to type a single key, say a 'w'
and have the java act upon it ( in a if/else type of way)
I have an arduino sending single chars mapped to 'w' or 'g'
when events happen, and I want the laptop to be the logger.