'Unreachable statement' error in java

0

I get an error without an apparent sense on line 78: (

  

78 | System.out.println ("Exit");

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

public class nochevieja{

public static Boolean verificar(String horario){
    //00:00
    final String numeracion = "0123456789";
    if(horario.length() != 5){ //Entrada sin sentido
        return false;
    }

    //Verificacion de la coherencia del horario
    for(int i = 0;i < 5;i++){
        if(i==2){
            continue;
        }
        Boolean numerico = false;
        for(int j = 0;j < numeracion.length();j++){
            if(horario.charAt(i) == numeracion.charAt(j)){ //Caracter actual es numerico
                numerico = true;
                break; //Sale del bucle porque el caracter acutal es numerico
            }
        }
        if(!numerico){ return false; }
    }
    int hora = 0,minutos = 0;
    if(horario.charAt(0)=='0'){ //Unidad hora
        hora = Integer.parseInt("" + horario.charAt(1));
    }
    if(horario.charAt(3)=='0'){ //Unidad minuto
        minutos = Integer.parseInt("" + horario.charAt(4));
    }
    else{
        minutos = Integer.parseInt("" + horario.charAt(3) + horario.charAt(4));
        hora = Integer.parseInt("" + horario.charAt(0) + horario.charAt(1));
    }
    System.out.print("");
    if(hora > 23 || hora < 0 || minutos >= 60 || minutos < 0){ //Digitos incorrectos
        return false;
    }
    return true;
} 
public static void main(String[]args){ //Funcion principal main
    Scanner scn = new Scanner(System.in);
    ArrayList<Integer> resultado = new ArrayList<Integer>(); //Vector de resultados

    System.out.println("Entradas: \n");

    int contador = 0;
    String horario = "hh:mm";

    //Entrada de datos
    while(true){
        contador += 1;
        System.out.print("["+contador+"]: ");
        horario = scn.nextLine(); //Entrada
        while(verificar(horario)){
            System.out.print("*["+contador+"]: ");
            horario = scn.nextLine(); //Entrada 
            if(horario.charAt(1) == '0' && horario.charAt(4)=='0'){
                break; //Rompe el bucle
            }
        }    
        int hora=0,minuto=0;
        if(horario.charAt(0)=='0'){ //Hora en unidad
            hora = Integer.parseInt(""+horario.charAt(1)); 
        }
        if(horario.charAt(3)=='0'){ //minuto en unidad
            minuto = Integer.parseInt(""+horario.charAt(0));
        }
        else{
            hora = Integer.parseInt(""+(horario.charAt(0)+horario.charAt(1)));
            minuto = Integer.parseInt(""+(horario.charAt(3)+horario.charAt(4)));
        }
        resultado.add(((24-hora)*60) + (60-minuto));  //Se caulcula el tiempo que falta
    }
    System.out.println("Salida");

    for(int i=0;i<resultado.size();i++){
        System.out.println("["+(i+1)+"] = "+resultado.get(i));
    }
}

}

    
asked by Walk Rock 23.06.2018 в 05:28
source

1 answer

1

The problem occurs because the cycle never ends.

while(true){
    ...
}

I recommend that you use a variable that can change its value when necessary. For example:

boolean bandera = true ;
while(bandera){
    ...
} 

This way you can control when your program changes state. You can read more about the use of flags here .

    
answered by 23.06.2018 в 18:07