initialize the size of an array with NULL

0

I am stuck with this topic, in a program with methods, we initialize array with NULL value because we still do not know the measure that it is going to have and we ask for the keyboard that indicates the measure, they give me this example that does not work, but if I enter measure = 2 the program asks 4 times the data, can you help me correct it?

//Variable global. Array no inicialitzat.
private int[] llistaEnters = null;
//En aplicar disseny descendent, ara cal declarar "lector" com a global
Scanner lector = new Scanner(System.in);
public static void main(String[] args) {
 OrdenarDescendentVariable programa = new OrdenarDescendentVariable();
 programa.inici();
}
public void inici() {
  llegirLlista();
  ordenarLlista();
  mostrarLlista();
 }
 //Mètode amb les instruccions per llegir la llista.
 //El primer valor sera la llargària
public void llegirLlista() {
 System.out.println("Escriu una llista de valors enters i prem retorn.");
 System.out.println("El primer valor indica la mida de la seqüència.");
 llegirMida();
 llegirValors();
}
public void llegirMida() { //Metode que llegeix el primer valor
 //Lectura
 int mida = 0;
 if (lector.hasNextInt()) {
  mida = lector.nextInt();
 } else {
  lector.next();
 }
 llistaEnters = new int[mida]; //Inicialitizació diferida de l'array
}
public void llegirValors() {
  int index = 0;
  while (index < llistaEnters.length) {
   if (lector.hasNextInt()) {
    llistaEnters[index] = lector.nextInt();
    index++;
   } else {
    lector.next();
   }
  }
  lector.nextLine();
 } //La resta de mètodes no canvien ...
}
    
asked by Carlos 20.02.2017 в 08:58
source

1 answer

1

Good morning, I have done some testing and the code that you have provided seems to work well. If I put length 4 it asks for 4 values, and with length n it asks me n ... I could not make it fail. Could it be that the failure was in the methods that do not happen to us? ( ordenarLlista() and mostrarLlista() )

I leave you the code of the tests that I have done in the same way:

import java.util.Scanner;

public class Main {

    //Variable global. Array no inicialitzat.
    private int[] llistaEnters = null;
    //En aplicar disseny descendent, ara cal declarar "lector" com a global
    Scanner       lector       = new Scanner(System.in);

    public static void main(String[] args) {
        Main programa = new Main();
        programa.inici();
    }

    public void inici() {
        llegirLlista();
        //ordenarLlista();
        //mostrarLlista();

        System.out.println("Llista de valors:");
        for (int valor : llistaEnters) {
            System.out.println(valor);
        }
    }

    //Mètode amb les instruccions per llegir la llista.
    //El primer valor sera la llargària
    public void llegirLlista() {
        System.out.println("Escriu una llista de valors enters i prem retorn.");
        System.out.println("El primer valor indica la mida de la seqüència.");
        llegirMida();
        llegirValors();
    }

    public void llegirMida() { //Metode que llegeix el primer valor
        //Lectura
        int mida = 0;
        if (lector.hasNextInt()) {
            mida = lector.nextInt();
        } else {
            lector.next();
        }
        llistaEnters = new int[mida]; //Inicialitizació diferida de l'array
    }

    public void llegirValors() {
        int index = 0;
        while (index < llistaEnters.length) {
            if (lector.hasNextInt()) {
                llistaEnters[index] = lector.nextInt();
                index++;
            } else {
                lector.next();
            }
        }
        lector.nextLine();
    } //La resta de mètodes no canvien ...
}
    
answered by 20.02.2017 / 09:24
source