Java - Clase propia "Teclado"

 
Vista:
sin imagen de perfil

Clase propia "Teclado"

Publicado por Iván (2 intervenciones) el 15/03/2015 16:56:02
Hola buenas,
hace unos días creé una clase Teclado desde la que recojo los tipos de datos que necesito.

La dejo por aquí:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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:
1
2
3
4
5
6
7
8
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):
1
2
3
4
5
6
7
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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Clase propia "Teclado"

Publicado por Iván (2 intervenciones) el 15/03/2015 18:50:45
Al final, lo que he hecho ha sido quitar todos los
Scanner teclado = new Scanner(System.in);
teclado.close();

Y colocar un
static Scanner teclado = new Scanner(System.in);

Justo después de la linea:
public class Teclado {
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar