Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Console{
- // método que lê uma String do teclado
- public static String leString(){
- BufferedReader teclado= new BufferedReader (new InputStreamReader (System.in));
- try{
- return teclado.readLine();
- } catch (IOException e){
- System.out.println(e.getMessage ());
- }
- return null;
- }
- // método que mostra uma mensagem na tela e lê uma String do teclado.
- // caso a String seja nula ou em branco, repete a amostragem e a leitura.
- public static String leString (String txt){
- String str="";
- while (true){
- System.out.print(txt);
- str=leString();
- if (str!=null && !str.trim().equals(""))
- return str.trim();
- }
- }
- // método que lê um int do teclado
- public static int leInt(){
- return Integer.parseInt (leString());
- }
- // método que mostra uma mensagem na tela e lê um int do teclado.
- // caso o valor digitado não seja um número, repete a amostragem e a leitura
- public static int leInt(String txt){
- while (true){
- try{
- System.out.print(txt);
- return leInt();
- }catch (NumberFormatException e){
- System.out.println("Numero Invalido");
- }
- }
- }
- // método que lê um double do teclado
- public static double leDouble(){
- return Double.parseDouble(leString());
- }
- // método que mostra uma mensagem e lê um double do teclado
- // caso o valor digitado não seja um número, repete a amostragem e a leitura
- public static double leDouble(String txt){
- while (true){
- try{
- System.out.print(txt);
- return leDouble();
- }catch (NumberFormatException e){
- System.out.println("Numero Invalido");
- }
- }
- }
- // método que apresenta um menu na tela e retorna o número da opção selecionada
- public static int menu(String titulo, String[] opcoes){
- int op = 0;
- while(true){
- System.out.println(titulo);
- for(int i = 0; i < opcoes.length; i++){
- System.out.println((i + 1) + ". " + opcoes[i]);
- }
- op = leInt("Opção: ");
- if (op > 0 && op <= opcoes.length)
- return op;
- else
- System.out.println("Opção inválida");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment