User input
Author |
Message |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Sun Jul 21, 2013 9:01 pm Post subject: User input |
|
|
Just a quick question, is there a function in Java that's like getch and hasch in turing? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Jul 21, 2013 9:05 pm Post subject: RE:User input |
|
|
You probably want the java.util.Scanner class, as long as you're using Java 1.5 (aka Java 5) or better. If you're using an ancient version of Java, such as the 1.4.2 that ships with Ready to Program, then you should upgrade, preferably to at least Java 6 (aka 1.6). |
|
|
|
|
![](images/spacer.gif) |
nullptr
|
Posted: Sun Jul 21, 2013 9:54 pm Post subject: Re: User input |
|
|
I'm not too familiar with Turing but it looks like getch is the same as in C++: you get a character without waiting for the end of the line. I'm pretty sure there's no way to do that in Java: System.in is an instance of InputStream ( http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html ) and the most low-level way to get a character is with System.in.read(). However, that method waits for the end of the line, so I think you're out of luck. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Jul 21, 2013 11:37 pm Post subject: Re: User input |
|
|
nullptr @ Sun Jul 21, 2013 9:54 pm wrote: ... most low-level way to get a character is with System.in.read(). However, that method waits for the end of the line, so I think you're out of luck.
False. InputStream.read() will read a single byte from its source (notably, not a character, which can be more than one byte).
Notably, though, Java's System.in is a typically attached to a line-oriented reader, and that is what will wait until the end of the line. I suppose it's possible to connect something else, but I've never seen that.
If you want to read keyboard input character by character, then you may want to look at AWT, Swing, or SWT. For example, try this: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html . If you aren't familiar with GUI programming, you may find that kind of thing difficult. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Jul 22, 2013 4:48 pm Post subject: RE:User input |
|
|
I'm familiar with event based programming if that's what you mean. Like visual basic, or ones of my own making |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Mon Jul 22, 2013 4:51 pm Post subject: RE:User input |
|
|
Thanks for the input. Learning new languages is so frustrating when I don't even know most syntax -.-' |
|
|
|
|
![](images/spacer.gif) |
|
|