Java Duvida?

1 resposta
R

Por que não consigo digitar o cep na seguencia

import java.io.*;
public class Teste2 {
    
    public static void main(String[] args) throws IOException{
        char nome=0;
        System.out.print("Nome:"+nome);
        nome= (char)System.in.read();
   
       int f=0;
       int g=0;
        while (f<=5){
    	System.out.print(" ");

 				f++;
}
char cep=0;
        System.out.print("Cep:"+cep);
        cep= (char)System.in.read();
   
	
	  while (g<=5){
    	System.out.print(" ");

 				g++;
	 
}
}
}

[size=“11”][color=“red”]* Editado: Lembre-se de utilizar BBCode em seus códigos para ficarem mais legíveis - Reifel[/color][/size] :joia:

1 Resposta

P

Você tem que ler o read do System.in em um loop e verificar quando for digitado um ENTER para facilitar e ir para a próxima linha:

import java.io.*; 
public class Teste2 { 
    
    public static void main(String[] args) throws IOException{ 
        System.out.print("Nome:");
        System.out.flush ();

        int c = System.in.read ();
        StringBuffer buffer = new StringBuffer();

        while (c != 13 && c != -1)
       {
           buffer.append ((char)c);
           c = System.in.read ();
       }
        
        System.out.print("CEP:");
        System.out.flush ();
        
        c = System.in.read ();

        while (c != 13 && c != -1)
       {
           buffer.append ((char)c);
           c = System.in.read ();
       }
        System.out.flush ();
        System.out.println("Nome e CEP: " + buffer.toString());
} 
}

Detalhe: eu acho melhor fazer um metodo só para leitura do que for digitado!!! Melhor do que ficar repetindo o mesmo LOOP toda hora.

blz!!! :grin:

Criado 20 de agosto de 2005
Ultima resposta 22 de ago. de 2005
Respostas 1
Participantes 2