Um classe que facilita a leitura de dados do teclahdo
Publicado por Dennis pelluzi 14/02/2006
[ Hits: 6.890 ]
Quem programa em outras linguagens, como C/C++, e está apredendo Java, depara-se com a seguinte situação: como atribuir valores para uma variável usando o teclado?
Em C, temos por exemplo o scanf() e em C++, o operador <<.
Em Java, também é possível adicionar um valor a uma variável usando o teclado, mas isso exige alguns procedimentos.
Eu criei uma classe Input que simplifique essa função. Ela ainda está incompleta (contribuições são bem-vindas), mas é uma mão na roda.
/** * Esta classe tem o propósito de facilitar a entrada de dados atraves do teclado. * É útil em programas que usam apenas o console. * Ainda está incompleta, pois falta tratar as exceções, mas funciona. * * @author: Dennis Guimaraes Pelluzi * @email: dennix@ibest.com.br * @version: 0.1, 14/02/2006 */ import java.io.*; public class Input { public static String getString() throws IOException { BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in)); String texto = new String(); texto = teclado.readLine(); return texto; } public static int getInt() throws IOException { BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in)); String texto = new String(); texto = teclado.readLine(); Integer inteiro = new Integer(texto); return inteiro.parseInt(texto); } public static long getLong() throws IOException { BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in)); String texto = new String(); texto = teclado.readLine(); Long longo = new Long(texto); return longo.parseLong(texto); } public static float getFloat() throws IOException { BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in)); String texto = new String(); texto = teclado.readLine(); Float num = new Float(texto); return num.parseFloat(texto); } public static double getDouble() throws IOException { BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in)); String texto = new String(); texto = teclado.readLine(); Double num = new Double(texto); return num.parseDouble(texto); } //Exemplo de utilização public static void main(String[] args) throws IOException { String name; int idade; float altura; System.out.println("Entre com o nome, idade e altura (m): "); name = Input.getString(); idade = Input.getInt(); altura = Input.getFloat(); System.out.println(name); System.out.println(idade); System.out.println(altura); } }
Calcula as chances de se ganhar na mega-sena.
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Visualizar arquivos em formato markdown (ex.: README.md) pelo terminal
Dando - teoricamente - um gás no Gnome-Shell do Arch Linux
Como instalar o Google Cloud CLI no Ubuntu/Debian
Mantenha seu Sistema Leve e Rápido com a Limpeza do APT!
Procurando vídeos de YouTube pelo terminal e assistindo via mpv (2025)
Sinto uma leve lentidão ao arrastar, miniminizar e restauras as janela... (0)
Zorin OS - Virtual Box não consigo abrir maquinas virtuais (6)
Iinstalar o Scanner Kodak i940 no Linux Mint 19/20? (3)