Comprobación de líneas en blanco al crear objetos extraños desde un archivo de texto

Tyler Rossi:

Estoy intentando crear objetos mediante la lectura de un archivo de texto dado estudiante, pero el archivo de texto tiene un montón de líneas en blanco vacía debajo de la información real. No se me permite editar el archivo de texto en modo alguno, y tampoco se me permite utilizar cualesquiera otros métodos además del escáner para la lectura del archivo. Todo funciona bien hasta que llegue a la parte inferior del archivo, donde una NoSuchElementExceptionse lanza y no el código.

        ArrayList<Student> Students = new ArrayList<>();
        while (fileIn.hasNextLine()) {
            Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
            student.setExam1(fileIn.nextInt());
            student.setExam2(fileIn.nextInt());
            student.setExam3(fileIn.nextInt());
            Students.add(student);
            fileIn.nextLine();
            System.out.println(student.toString());
        }

Las miradas de archivo como este

Peck    CSI1000 12345   97  76  72
Rhodes  CSI1000 87649   98  70  73
Rinke   CSI1000 87649   78  70  78
Romanski    CSI1000 87649   84  84  95
Rombach CSI1000 12345   82  86  96
Ruan    CSI1000 87649   70  94  76
Ruc CSI1000 87649   97  98  80
Scott   CSI1000 87649   90  80  73
Shah    CSI1000 87649   98  72  71
Teal    CSI1000 87649   89  72  99
Tingley CSI1000 87649   76  85  72
Towne   CSI1000 87649   71  71  100
Tucker  CSI1000 12345   89  71  93
Ureel   CSI1000 87649   76  80  99
Wallace CSI1000 87649   76  100 71
Weber   CSI1000 87649   75  73  75
Wierszewski CSI1000 12345   93  90  72
Wilmot  CSI1000 12345   85  96  74
Wilson  CSI1000 12345   91  75  97
Yang    CSI1000 87649   83  80  85
Yasoni  CSI1000 87649   78  90  76









JRowan:

puede tratar de capturar la excepción

    ArrayList<Student> Students = new ArrayList<>();
            while (fileIn.hasNextLine()) {
            try{
                Student student = new Student(fileIn.next(), fileIn.next(), fileIn.nextInt());
                student.setExam1(fileIn.nextInt());
                student.setExam2(fileIn.nextInt());
                student.setExam3(fileIn.nextInt());
                Students.add(student);
                fileIn.nextLine();
                System.out.println(student.toString());
            }catch(NoSuchElementException e){
                //break;
                fileIn.nextLine();

                System.out.println("exception");
            }
            }

o puede simplemente breakdentro del bloque catch si después de la excepción no está esperando más información de todos modos

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=358286&siteId=1
Recomendado
Clasificación