Um classe que facilita a leitura de dados do teclahdo
Publicado por Dennis pelluzi 14/02/2006
[ Hits: 6.651 ]
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); } }
Código para validar CPF e CNPJ otimizado
Ordenação de vetores com letras do alfabeto (atualizado)
Determinante de uma matriz de ordem 3.
Gerador de números aleatórios em Java
Como escolher o melhor escalonador de CPU para melhorar o desempenho da máquina
Curiosidade sobre DOOM Guy e Isabelle de Animal Crossing
Inicializando servidor Ubuntu na AWS e rodando apache em Container
Conheça o Octopi, outro frontend para o Pacman com acesso ao ARU (Arch Linux e derivados)
Terminal transparente no Debian 12 com interface i3wm usando Xfce4-Terminal e Compton
Como compilar o DOOM RETRO no Linux
Stacer - um otimizador gráfico básico de Linux
Corrigindo o erro de "WARNING: Possibly missing firmware for module" no Arch Linux
Ubuntu simplesmente morreu (7)
Como personalizar meu Linux Mint xfce? (10)
DRIVER DE ADPTADOR HDMI PARA USB (1)