Why does not the Scanner read the variables and sometimes send an exception?

2

I have the following problem: when running the program the menu is shown with all its options but when choosing the options, it only takes the number 1 and the 5 that ask for data, after entering these data, two pass things:

  • It does not do anything when pressing enter and keeps asking infinitely
  • Send the exception: InputMismatchException .
  • They could say where the error is so as not to commit it in future programs would help me a lot

    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
            int opc = 0;
            Altura altura = new Altura();
            Scanner leer = new Scanner(System.in);
            Scanner lee = new Scanner(System.in);
            System.out.println("                 .::BIENVENIDO AL SISTEMA::.");
            while (10 != (opc)){
                opc = 0;
            System.out.println("introduzca la opcion que desea utlizar:");
            System.out.println("1#para introduzir una altura");
            System.out.println("2# para sacar la altura mas alta introduzida");
            System.out.println("3# para la altura mas baja intoducida");
            System.out.println("4# para sacar le promedio de las alturas");
            System.out.println("5# mostrar la alturas mayores a una indicada por el usuario");
            System.out.println("10# salir del sistema");
            opc = leer.nextInt();
              switch (opc){
                case 1 :altura.insertar_alt();
                        break;
                case 2 :altura.alt_may();
                        break;
                case 3 :altura.alt_men();
                        break;
                case 4 :altura.altura_pro();
                        break;
                case 5 :altura.mostrar_altmay();
                        break;
            }
              /**  if (1 == (opc)) {
                    altura.insertar_alt();
                }
                if (2 == (opc)) {
                    altura.alt_may();
                }
                if (3 == (opc)) {
                    System.out.println("la altura menor es: "+altura.alt_men());
                }
                if (4 == (opc)) {
                    System.out.println("el romedio de latura es: "+ altura.altura_pro());
                }
                if (5 == (opc)){
                    altura.mostrar_altmay();
                }
            **/}
            System.out.println("                      .::SALIENDO DEL SISTEMA::.");
        }
    }
    
      

    This is the class where the data is requested:

        public class Altura {
        double arre_altura[] = new double[10];
        Scanner lect = new Scanner(System.in); 
    
        public void Altura(){
            for (int i = 0; i < 10; i++) {
                arre_altura[i] = 0;
            }
            }
    
        public int vacio(){ //si retorna 1 es vacio 
          if (arre_altura[0]==0){
              return 1;
              }else{
              return 0;
          }
          }
    
        public int lleno(){ //si retorna 1 esta lleno
          if (arre_altura[9]!=0){
              return 1;
              }else{
              return 0;
          }
          }
             public int pos_v(){ //metodo que busca la primera poscicion vacia
           int pos;
           pos = -1;
           int i = 0;
           while ((pos == -1) || (i<10)) {
               if (0 == arre_altura[i]){
                   pos = i;
               }
           }
            return pos;
             }
    
         public void insertar_alt(){
           String altura = "0";   
           if (lleno()==1) {
               System.out.println("no se han encontrado mas posiciones para almacenar una altura");
                }else{
                    System.out.println("inserte la altura que desea registrar: ");
                    try{
                    altura = lect.nextLine();
                               }catch (java.util.InputMismatchException e) {
                   System.out.println("el error es: "+ e );
               }
                    double doble = Double.parseDouble(altura);
    
                       arre_altura[pos_v()] = doble;
                       }
    
       }
    
        public void alt_men(){   //metodo que devuelbe la altura mas baja//
                double alt = arre_altura[0];
                if (vacio()==1){
                    System.out.println("el arreglo esta vacio");
                }else{
          for (int i = 0; i < 10; i++) {
              if (alt < arre_altura[i]){
                  alt = arre_altura[i];
    
              }
          }
    
              System.out.println("la menor altura introduzida es: "+ alt);
                }        
            }
        public void altura_pro(){ //calculo del promedio de altura//
          double total= 0;
          int acum = 0;
          for (int i = 0; i < 10; i++) {
              if (arre_altura[i] != 0){
              total = total + arre_altura[i];
                   acum = acum + 1;
              }
          }
    
            System.out.println("el promedio de las alturas es: "+ total);
         }
        public void mostrar_alt(){
          for (int i = 0; i < 10; i++) {
              if (arre_altura[i]!=0) {
                System.out.println("La altura en el espacio "+i+" es: "+arre_altura[i]);
              }
          }
         }
    
        public void mostrar_altmay(){
            double i = 0;
            System.out.println("introduzca la altura que desea verificar: ");
            try{
            i = lect.nextDouble();
            }catch (java.util.InputMismatchException x){ 
                System.out.println("el error es: "+ x );
            }
            for (int j = 0; j < 10; j++) {
                if (i > arre_altura[j]){ 
                    System.out.println("altura:"+ arre_altura[j]);                
                }
            }
            System.out.println("esas son las alturas (en caso de no haber encontrado una altura mayor no se habramotrado nada)");
        }
    
        
    asked by alejandro wolfgang 19.03.2018 в 16:24
    source

    2 answers

    3

    The problem is in this method pos_v that you call when reading the height in the method insert_alt (), since it remains in an infinite cycle, because you declare the variable pos = -1 when starting the method, then in the while you place in the condition pos == -1, and this will never change repeating the cycle infinitely

    public int pos_v(){ //metodo que busca la primera poscicion vacia
       int pos;
       pos = -1;
       int i = 0;
       while ((pos == -1) || (i<10)) { //Ciclo infinito: variable pos siempre sera igual a -1
           if (0 == arre_altura[i]){
               pos = i;
           }
       }
        return pos;
         }
    
        
    answered by 19.03.2018 / 18:38
    source
    0

    In case 2 of the menu, you are calling the alt_may () function of the height object, but you did not define it in the Height class:

      

    case 2: height.alt_may ();                       break;

    That is the cause of the error.

    Solution:
    Define the alt_may () function in the Height class to be able to use it.

        
    answered by 19.03.2018 в 17:03