Pegar um inteiro

4 respostas
F

Ola pessoal,

Como faco para pegar um numero pelo teclado???

4 Respostas

smota

Que tal procurar no histórico do Forum?
(é o minimo antes de postar uma duvida)

http://www.guj.com.br/forum/viewtopic.php?t=289&highlight=ler+teclado
http://www.guj.com.br/forum/viewtopic.php?t=325&highlight=ler+teclado

Pra acrescenter veja a classe Keyboard

D

Se for pelo console, dê uma pesquisada aqui no fórum que eu já vi discussões sobre isso.

Se for uma app Swing, você precisa registrar um ouvinde de tecla (KeyListener) passando objeto KeyStroke com a tecla que deverá disparar o evento configurada. Também já vi mensagens sobre isso no aqui.

Dê uma pesquisada que tu acha. :slight_smile:

Abraços.

N

Para ler do prompt, você pode fazer algo como:

byte buffer[] = new byte[255];
System.in.read(buffer); // le do teclado o que você digitar
String str = new String(buffer);
meuInteiro = Integer.parseInt(str);
Daniel_Quirino_Olive

byte buffer[] = new byte[255]; //buffer overflow à vista... System.in.read(buffer); // le do teclado o que você digitar String str = new String(buffer); meuInteiro = Integer.parseInt(str);

why not?

ObjectInputStream ois = new ObjectInputStream(System.in); int meuinteiro = ois.readInt(); //Integer meuinteiro = (Integer)ois.readObject();

:slight_smile:

documentação: http://java.sun.com/j2se/1.4.1/docs/api/java/io/ObjectInputStream.html

e isso é tudo, p-pessoal :lol:

Criado 10 de abril de 2003
Ultima resposta 11 de abr. de 2003
Respostas 4
Participantes 5