dq28121989
Goto Top

Wie in Java nur einzelnes Zeichen einlesen? (wie Readkey in Pascal)

Hallo, ich würde gerne wissen wie man möglichst "Unkompliziert" nur ein einzelnes Zeichen einlesen kann ohne Enter drücken zu müssen. In Pascal gab es eine nette funktion die sich Readkey(); nannte.

Beispiel für die Anwendung:
menu=Tastatur.liesInt();//<-----------------hier soll nur eine Taste eingelesen werden
switch(menu)
{
case 1:
{

break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 0:
{
System.out.println("Auf Wiedersehen!");
break;
}
default:
{
System.out.println("Falsche Eingabe!");
break;
}

}


Ich hoffe ihr habt eine einfache Lösung für mein Problem.

Content-Key: 133089

Url: https://administrator.de/contentid/133089

Printed on: April 26, 2024 at 17:04 o'clock

Member: maretz
maretz Jan 10, 2010 at 20:58:53 (UTC)
Goto Top
die frage ist erstmal: baust du ne konsolen-anwenung, eine gui oder was auch immer?

das dürfte dir aber ggf. helfen: http://packard.flint.umich.edu/~hickslm/313/exceptions/GetChar.html
Member: dq28121989
dq28121989 Jan 10, 2010 at 21:30:46 (UTC)
Goto Top
Anhand von System.out.println(); sollte man erkennen das es sich um eine Konsolenanwengung handelt.

Dein Link sieht sehr interessant aus, allerdings funktioniert der quelltext nicht, mein Compiler gibt mir 22 Errors aus.
Ansonsten Danke schonmal.


Dieser Code hilft mir schon etwas weiter:
static public char getChar() throws IOException
{
char ch = (char) System.in.read();
flushInput(); // This clears out System.in
return ch;
}

Nur gibt es da Probleme.
-Flushinput() existiert scheinbar nicht (auch nicht in System.in.flushinput();)
Wenn mir damit jemand helfen könnte wäre das Problem gelöst.

lg Steven
Member: maretz
maretz Jan 11, 2010 at 06:42:48 (UTC)
Goto Top
Nö - nur weil du nen Sys-Out-Println verwendest is das noch lang keine Konsolenanwendung... Du wirst dich wundern - ich nutze sowas in meiner Anwendung auch oft (z.B. in den SQL-Klassen) damit ich einfach ne kleine Debug-Ausgabe auf die (Eclipse-)Konsole zaubere... Dies hat den Vorteil das es eben nicht gleich in der Anwendung aufpoppt - und mich 30 Kollegen anrufen was denn die komische Meldung da auf dem Schirm soll....
Member: Marco123
Marco123 Jan 11, 2010 at 15:37:27 (UTC)
Goto Top
http://openbook.galileodesign.de/javainsel7/javainsel_04_007.htm

den Rest darfst du alleine machen, dann lernst du auch etwas noch dabei :- )
Member: dq28121989
dq28121989 Jan 11, 2010 at 16:03:22 (UTC)
Goto Top
@maretz: okay dann entschuldige meine Bemerkung
Member: dq28121989
dq28121989 Jan 11, 2010 at 16:05:25 (UTC)
Goto Top
Das ist noch nicht das was ich suche... Zeichenketten zerlegen ist eine andere Sache wie ein einzelnes Zeichen einlesen. Ich möchte z.b. die taste 1 drücken und das Programm läuft weiter. In dem Fall (wenn ichdas so richtig verstanden habe) muss ich erst enter drücken und das ist das was ich nicht haben möchte.
Mitglied: 76109
76109 Jan 11, 2010 at 19:02:21 (UTC)
Goto Top
Hallo dq28121989!

Ich habe zwar keine Ahnung von Java, aber eventuell funktioniert das hier:

char buffer;

System.in.read(buffer, 0, 1);

switch(buffer)
{
Case '1':  break;  
...
}
Lese in Buffer beginnend ab Offset 0 ein Zeichen (Byte) ein.

Und mit Case z.B. nicht auf Integer 1 sondern auf Char '1' prüfen

Gruß Dieter
Member: Marco123
Marco123 Jan 11, 2010 at 19:20:19 (UTC)
Goto Top
Zitat von @76109:
Hallo dq28121989!

Ich habe zwar keine Ahnung von Java, aber eventuell funktioniert das hier:

> char buffer;
> 
> System.in.read(buffer, 0, 1);
> 
> switch(buffer)
> {
> Case '1':  break;  
> ...
> }
> 
Lese in Buffer beginnend ab Offset 0 ein Zeichen (Byte) ein.

Und mit Case z.B. nicht auf Integer 1 sondern auf Char '1' prüfen

Gruß Dieter



Das bringt nichts :- )

das Problem ist das blockierende lesen:
die System.in.read(); methode wartet auf ein ENTER.

Ob du den Wert in ein Byte Array oder in eine Variable oder sonst wo hin schiebst ist egal ,)

er brauch eine andere Methode um den Stream zu lesen.

mir fallen nur die Methode ein:
System.in.read() -> ist blockierend also nein.
Console -> keine Ahnung. glaube aber ist auch blockierend.


ODER:
Du schreibst dir ne GUI Applikation, und implementierst Actionlistener.
Sobald der User etwas schreibt holst du dir den Text.

Aber von der Console aus zu lesen fällt mir auf die schnelle nichts ein.
Mitglied: 76109
76109 Jan 11, 2010 at 19:31:21 (UTC)
Goto Top
Hallo Macro!

Naja, ich war der Meinung, dass der Parameter für nur 1 Zeichen einlesen kein Enter erforderlich macht. Da habe ich mich wohl geirrtface-smile

Gruß Dieter
Member: Marco123
Marco123 Jan 11, 2010 at 19:42:39 (UTC)
Goto Top
habs eben mal ausprobiert, jedoch musste ich trotzdem enter drücken ,)

hier die defination der Methode:

int java.io.InputStream.read(byte b, int off, int len) throws IOException

read
public int read(byte b,
int off,
int len)
throws IOException
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b through b[off] and elements b[off+len] through b[b.length-1] are unaffected.

The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method. If any subsequent call to read() results in a IOException, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored into b and the number of bytes read before the exception occurred is returned. The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method.


Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
See Also:
read()
Member: dq28121989
dq28121989 Jan 12, 2010 at 11:12:40 (UTC)
Goto Top
könntest du mir einen Link dazu schicken? Den Copy-Paste-Salat kann man ja nicht lesen. Der code verteilt sich auf mehrere zeilen.
Ist die Methode für Gui oder konsolenanwendungen?
Member: Marco123
Marco123 Jan 12, 2010 at 11:30:32 (UTC)
Goto Top
Hallo,

dies war die Erklärung die mir Eclipse dazu geliefert hat.
Falls du kein Eclipse installiert hast, schau mal ansonsten in google nach java.io.read
Member: dq28121989
dq28121989 Jan 12, 2010 at 11:34:05 (UTC)
Goto Top
okay danke das ist schonmal ein guter ansatz