Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/03/2015, 08:52
elsaylala
 
Fecha de Ingreso: septiembre-2010
Mensajes: 55
Antigüedad: 13 años, 7 meses
Puntos: 0
Clase propia "Teclado"

Hola buenas,
hace unos días creé una clase Teclado desde la que recojo los tipos de datos que necesito.

La dejo por aquí:
Código:
package teclado;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Teclado {
	// //////////////////////// INT //////////////////////// //
	// Int cualquiera
	public static int numInt() {
		boolean error;
		int numero = 0;
		Scanner teclado = new Scanner(System.in);
		do {
			try {
				numero = teclado.nextInt();
				error = false;
			} catch (InputMismatchException e) {
				System.out.println("Introduce un numero valido.");
				error = true;
				teclado.nextLine();
			}
		} while (error);
		teclado.close();
		return numero;
	}

	// Int que se compare con un valor mínimo/máximo pasado como parametro
	public static int numInt(int tipo, int maxmin) {
		boolean error;
		int numero = 0;
		Scanner teclado = new Scanner(System.in);
		do {
			try {
				numero = teclado.nextInt();

				if (tipo == 1 & numero >= maxmin)
					error = false;
				else if (tipo == 2 & numero <= maxmin)
					error = false;
				else if (tipo == 3 & numero > maxmin)
					error = false;
				else if (tipo == 4 & numero < maxmin)
					error = false;
				else {
					error = true;
					System.out.println("Introduce un numero valido.");
					teclado.nextLine();
				}

			} catch (InputMismatchException e) {
				System.out.println("Introduce un numero valido.");
				error = true;
				teclado.nextLine();
			}
		} while (error);
		teclado.close();
		return numero;
	}

	// Int que debe estar entre un valor mínimo y un valor máximo admisibles,
	// que se le pasarán como parámetros
	public static int numInt(int tipo, int min, int max) {
		boolean error;
		int numero = 0;
		Scanner teclado = new Scanner(System.in);
		do {
			try {
				numero = teclado.nextInt();
				error = false;

				if (tipo == 1 & numero >= min & numero <= max)
					error = false;
				else if (tipo == 2 & numero > min & numero < max)
					error = false;
				else if (tipo == 3 & numero >= min & numero < max)
					error = false;
				else if (tipo == 4 & numero > min & numero <= max)
					error = false;
				else {
					error = true;
					System.out.println("Introduce un numero valido.");
					teclado.nextLine();
				}

			} catch (InputMismatchException e) {
				System.out.println("Introduce un numero valido.");
				error = true;
				teclado.nextLine();
			}
		} while (error);
		teclado.close();
		return numero;
	}
}

Y haciendo pruebas en un Main como:
Código:
package teclado;

public class Main {
	public static void main (String args []){
		System.out.println(Teclado.numInt(1, 18, 30));
		System.out.println(Teclado.numInt(1, 18, 30));
	}
}

Me da el siguiente error a la hora de pedirme el segundo dato (no llega a pedirlo):
Código:
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at teclado.Teclado.numInt(Teclado.java:364)
	at teclado.Main.main(Main.java:6)

Estoy empezando con java y seguramente sea un error la mar de tonto, pero estoy que no sé... Gracias