Help in a list in Java

1

This is the fragment of my code:

public static void Lista(String textList) { 
        String[]stList= textList.split(",");
          Arrays.asList(stList);
              for(int i = 0; i < stList.length; i++){
          System.out.println("Escribe un String para convertirlo a List: ");
                  if((stList == null) || (stList.equals(""))){
                      System.out.println(" El campo que ingreso esta vacio, vuelva a ingresarlo");
                  }else{
                    System.out.println(stList[i]);
              }
        }
    }

It's a list, which I'm dealing with the If and Else commands that tell me that the field is empty and that it shows the message again: "Write a String to convert it to List:"

    
asked by Reinos Ric 02.05.2018 в 23:31
source

1 answer

2

What happens is that your code does not enter if since you are comparing all the array to null it must have been compared with the position in which you are iterating the cycle so: stList[i] , if you want to ask for the chain again you can do it within a cycle while , adapting a bit your code would be as follows:

private static Scanner teclado = new Scanner(System.in);
public static void main(String[] args) {
    String s = ""
    String continuar = "Si";
    //Se hace un ciclo para volver a pedir la cadena a evaluar.
    while(continuar.equalsIgnoreCase("si")){
        //Se pide el string por teclado que se va a pasar al método.
        System.out.println("Ingrese una cadena: ");
        s = teclado.nextLine();
        Lista(s);
        System.out.println("Desea continuar?: Si/No ");
        continuar = teclado.nextLine();
    }

}

public static void Lista(String textList) {
    String[] stList = textList.split(",");
    Arrays.asList(stList);
    for (int i = 0; i < stList.length; i++) {
        //Pregunto si la posición esta vacía
        if (stList[i] == null || stList[i].trim().equals("")) {
            //Si esta vacia pido el nuevo valor a tomar en la posición
            System.out.println("El campo que ingreso esta vacio, vuelva a ingresarlo");
            stList[i] = teclado.nextLine();
            //Regreso la posición de i en 1 para que imprima el nuevo valor introducido a la posición
            i = i-1;
        } else {
            System.out.println(stList[i]);
        }
    }
}
    
answered by 03.05.2018 / 00:12
source