Error in reading data type INT with JAVA Scanner [Resolved]

1

I have a problem reading data int with 'nextInt' and I do not know what it can be. I leave the method that is giving me the error; If something is missing or you want the complete code, you can ask me. Could you tell me what it can be?

static int numero_DNI;
static ArrayList<Integer> DNI = new ArrayList <Integer>();
static boolean comprobar;
protected ArrayList<Integer> ImportarDNI() {
    Scanner sc = new Scanner (System.in);

    numero_DNI = sc.nextInt();
    DNI.add(numero_DNI);

    if (DNI.size() > 7 && DNI.size()<0) {
        System.err.println("El DNI debe tener los 7 números");
        comprobar = false;
    } else {
        comprobar = true;
    }

    sc.close();
    return DNI;
}

Errors:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Personas.Persona.ImportarDNI(Persona.java:30)
at Personas.Persona.main(Persona.java:64)
    
asked by unanobot 23.05.2018 в 10:06
source

2 answers

1

You could do this. I have left some comments:

package com.test.victor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class TestClass {

    public static void main(String[] args) {
        ImportarDNI();
    }

    private static String numero_DNI;
    private static ArrayList<Integer> DNI = new ArrayList<Integer>();
    private static boolean comprobar;

    private static final Set<Character> numbers = new HashSet<>();
    static {
        numbers.add('0');
        numbers.add('1');
        numbers.add('2');
        numbers.add('3');
        numbers.add('4');
        numbers.add('5');
        numbers.add('6');
        numbers.add('7');
        numbers.add('8');
        numbers.add('9');
    }

    private static ArrayList<Integer> ImportarDNI() {
        Scanner sc = new Scanner(System.in);

        // He añadido esta línea para que el usuario introduzca un número
        System.out.println("Introduzca el numero: ");

        numero_DNI = sc.nextLine().trim();

        // No puede sser mayor tue 7 y menor que cero al mismo tiempo: DNI.size() > 7 &&
        // DNI.size() < 0
        // Mejor utilizar la función: checkRightInput(String input)
        if (numero_DNI.length() != 7) {
            System.err.println("El DNI debe tener los 7 números");
            comprobar = false;
            sc.close();
            // Devolvemos la ArrayList<>() vacía
            return new ArrayList<>();
        } else {
            if (checkRightInput(numero_DNI)) {
                // Al comprobarlo podemos convertir la String en Integer y agregarlo a DNI
                Integer tested = Integer.valueOf(numero_DNI);
                DNI.add(tested);
                System.out.println("DNI introducido correctamente");
                comprobar = true;
                sc.close();
                return DNI;
            } else {
                comprobar = false;
                sc.close();
                System.out.println("No se han podido introducir los datos");
                // Devolvemos la ArrayList<>() vacía
                return new ArrayList<>();
            }
        }

    }

    private static boolean checkRightInput(String input) {

        char[] chars = input.toCharArray();

        for (char c : chars) {
            // Comprobar que cada caracter es un numero entre 0 y 9
            if (!numbers.contains(c)) {
                System.out.println("El caracter '" + c + "' no representa un número");
                return false;
            }
        }
        return true;
    }

}
    
answered by 23.05.2018 / 10:26
source
0

I have implemented your problem in the following way:

import java.util.ArrayList;
import java.util.Scanner;

public class main {
int numero_DNI;
ArrayList<Integer> DNI = new ArrayList <Integer>();
boolean comprobar;
public static void main(String[] args) {
ArrayList<Integer> dnis= new ArrayList<Integer>();
do {

    main m = new main();
    dnis=m.ImportarDNI();
}while(true);




}

public ArrayList<Integer> ImportarDNI() {
Scanner sc = new Scanner (System.in);
System.out.println("Introduce el DNI");
    numero_DNI = sc.nextInt();
    DNI.add(numero_DNI);

    if (DNI.size() > 7 && DNI.size()<0) {
        System.err.println("El DNI debe tener los 7 números");
        comprobar = false;
    } else {
        comprobar = true;
    }

    return DNI;
}       

}

And it does not give any problem. You have the problem when closing the data entry flow, remove that line and let java manage it as you see fit. Another possible problem you may have is that you try to enter a letter .. at the end of the numbers, to do that you will have to use nextLine() and the last of the errors you can have is that you exceed the number of digits allowed.

    
answered by 23.05.2018 в 10:25