Leitura de Strings

3 respostas
Y
Como eu faço uma leitura de String via teclado

Quando são valores inteiros posso fazer desta forma, com String não   sei.

System.out.println(Entre com a Idade:);

aux=teclado.readLine();

objPessoa.Idade=Integer.parseInt(aux);
System.out.println(Entre com o Nome da Pessoa:);

aux=teclado.readLine();

objPessoa.Nome = String.parseString(aux);  <-------  ??

3 Respostas

S

O default é String, não precisa fazer conversão nenhuma.
Só é necessária conversão para outros tipos.

W
Esse sisteminha meu professor passou na faculdade para fazer leitura do teclado via console.
public class Console{
   public static double readDouble(){
      try{
         BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
         String s = br.readLine();
         Double d = new Double(s);
         return (d.doubleValue());
      }
      catch (NumberFormatException e){
         return (0);
      }
      catch (IOException e){
         return (0);
      }
   }
   public static float readFloat(){
      try{
         BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
         String s = br.readLine();
         return (Float.parseFloat(s));
      }
      catch (NumberFormatException e){
         return (0);
      }
      catch (IOException e){
         return (0);
      }
   }

   public static int readInteger(){
      try{
         BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
         String s = br.readLine();
         return (Integer.parseInt(s));
      }
      catch (NumberFormatException e){
         return (0);
      }
      catch (IOException e){
         return (0);
      }
   }
   public static String readString(){
      try{
         BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
         String s = br.readLine();
         return (s);
      }
      catch (IOException e){
         return ("");
      }
   }
}
Y

Ah agora entendi,deu certo aqui…
Obrigado pelas respotas…
:grin: :grin:

Criado 19 de maio de 2005
Ultima resposta 19 de mai. de 2005
Respostas 3
Participantes 3