This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Input program  RSS feed

 
Jon Pearson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class ReadKeyboard {
char in;
public void aMethod() {
try {
System.out.println("pausing- press enter");
in = (char) System.in.read();
return;
}
catch (IOException e) {}
finally {
System.out.println("That's all folks");
}
return;
}
public static void main (String [] args) {
ReadKeyboard rk= new ReadKeyboard();
rk.aMethod();
}
}
When this is compiled and run, it outputs "Pausing press enter" and after the enter key is pressed: "Thats all folks" .I'm trying to alter it so that you have to press "1" to get the second message, instead of pressing enter, but when I try to alter the 'char' and 'in' parts I can't get it to compile.I'm a bit of a Java novice as you will have guessed. Thanks for any help.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...
I'm not sure of a simple way to do this. The problem is that the data entered in the console is not available until the "enter" key is pressed. What you need is a KeyListener assigned to the console, but since the console is not an AWT component, I don't think that you can do this.
There are alternatives, however. If you need the "KeyListener" functionality, you can create your own console using AWT/Swing (do a Google search and you will find any number of examples).
Or, alterantively, you can make your user press "enter" after they press "1."
[ May 13, 2003: Message edited by: Joel McNary ]
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am not sure how to make char to be read with out enter. But i hope this code might help you in some way this is an extension of your code.
import java.io.*;
public class ReadKeyboard
{
char in;
public void aMethod()
{
try
{
System.out.println("pausing- press enter");
StringBuffer input = new StringBuffer();
while( true )
{
in = (char) System.in.read();
if( in == '1')break;
input.append( in );
}
System.out.println("Key board input is " + input);
return;
}
catch (IOException e) {}
finally
{
System.out.println("That's all folks");
}
return;
}
public static void main (String [] args)
{
ReadKeyboard rk= new ReadKeyboard();
rk.aMethod();
}
}
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jon
I think that it is impossible if you only type "1" and without type enter to display the second message, as I know that System.in.read() act as a buffer which must be pressed enter to flush the data(exception for ctr-C or ctr-Z or some kind illegal action to terminate )
May be I misunderstand what you said,but anyway, I can use the conditional checking to know what the user type.

Hope this help

[ May 14, 2003: Message edited by: siu chung man ]
 
Jon Pearson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the user presses '1' and then enter then that will be fine.I'm writing a banking program where they press '1' and it goes to the withdraw method, or if they press '2' then it goes to a deposit method.We aren't allowed to use Swing unfortunately, or I would do radio buttons or something.
Anyway thanks for the advice guys, much appreciated.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon
If you use console to read something,it must contain the buffer,so you must press enter key, but you can try to use timer. But you should know how to flush the input data and how to use the timer.But in your case,it has some limitation because it is not automatically to read your input when you type the data/integer at the same time.
I regret that I have not any idea on this case
The console can read data when the user type data and then without press the enter key that can not use JFC/Swing.
[ May 14, 2003: Message edited by: siu chung man ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!