1

Eclipse spots them as unreachable code, which apparently is a code that will never be read for there's no path to reach it, but I don't see why. The instructions are inside a main() method

    //leemos
    FileInputStream fis;
    ObjectInputStream ois;
    Alumno alumnoLeido = null;
    String cadena ="";
    JTextArea area = new JTextArea(6,1);
    while(true){
        try {
            fis = new FileInputStream("alumnos.txt");
            ois = new ObjectInputStream(fis);
            alumnoLeido = (Alumno) ois.readObject();
            ois.close();
            cadena = "Alumno " + alumnoLeido.getNombre() + " " + alumnoLeido.getApellido() + " vive en " 
            + alumnoLeido.getDireccion() + " y tiene una beca de " + alumnoLeido.getBeca() + " euros \r\n";
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    area.append(cadena);
    JOptionPane.showMessageDialog(null, area, "Alumnos",1);
Artur Alvaro
  • 115
  • 8
  • while(true) - True is always true unless it's forced to be 'false' or make it to 'break'. – Sajal Dutta Oct 09 '15 at 02:03
  • 1
    @SajalDutta What do you mean by `unless it's forced to be 'false'`? Are you saying that the primitive `boolean` value `true` can be changed to `false`? –  Oct 09 '15 at 02:11
  • How is it ever going to exit the while loop? It is always going to be true, so the lines after the while loop would never be reached. – Dijkgraaf Oct 09 '15 at 02:19
  • @JimmyStallion You can use a boolean variable which can be initially true and inside the while scope the value of the variable can be changed. Hence, terminating while loop or just use break when you need to get out of the loop. – Sajal Dutta Oct 09 '15 at 02:59

2 Answers2

3

They are unreachable because your while loop will never terminate.

iagreen
  • 31,470
  • 8
  • 76
  • 90
3

while(true) is an infinite loop. Without a break, the loop will never terminate and allow the following code to execute. So, you will never reach the remaining statements.

elixenide
  • 44,308
  • 16
  • 74
  • 100