Problems with a Loop

1

Hello, you previously helped me with this part 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]);
              }
        }
    }

Which I now have this little detail: I would like the if(toList.contains(",")) statement to be repeated every time the comma (,) is not inserted. Since the "Falta el carácter coma (,) por favor ingreselo:" message is repeated only once, I know that a loop must be returned, but the command which is while has not worked out well. Please help me.

    
asked by Reinos Ric 04.05.2018 в 23:51
source

4 answers

3

It must be inside the while loop, try the following:

String toList="";
System.out.println("Escribe un String para convertirlo a List: ");          
while(true){
    toList= br.readLine();
    if( (toList == null) || (toList.trim().equals("")) ){
        System.out.println("El campo que ingreso esta vacio, vuelva a ingresarlo:");
        continue;
    }
    if(!toList.contains(",")){
        System.out.println("Falta el caracter coma (,) por favor ingreselo:");
        continue;                   
    }
    CorreoUtilBuffereader1.Lista(toList);
    break;
}

Of course if you like to see each message separately, otherwise the answer of Alan McCall is enough.

    
answered by 05.05.2018 / 01:44
source
2

Based on the question:

  

Help in a list in Java

I leave you the following answer:

public static void Lista(String textList) {
        String[] stList = textList.split(",");
        Arrays.asList(stList);
        String lista = "";
        for (int i = 0; i < stList.length; i++) {
            if (stList[i] == null || stList[i].trim().equals("")) {
                System.out.println("El campo que ingreso esta vacio, vuelva a ingresarlo");
                lista = teclado.nextLine();
                //Se valida si el campo no contiene la ,
                while(!lista.contains(",")){
                    System.out.println("El campo no contiene (,) vuelva a ingresarlo");
                    lista = teclado.nextLine();
                }
                stList[i]=lista;
                i = i-1;
            } else {
                System.out.println(stList[i]);
            }
        }
    }
    
answered by 05.05.2018 в 00:02
1

You can do it like this:

String toList="";
System.out.println("Escribe un String para convertirlo a List: ");          
            toList= br.readLine();
            while((toList == null) || (toList.trim().equals(""))  || !(toList.contains(","))){
                  System.out.println("El campo que ingreso esta vacio, o falta el caracter coma(,) vuelva a ingresarlo:");
                  toList= br.readLine();
            }
            CorreoUtilBuffereader1.Lista(toList);
    
answered by 04.05.2018 в 23:58
1

You could use a do-while loop to validate what was entered by the user. With this loop you will be able to make it run at least once and if in the validation of the input by the user it turns out that you must enter information again, so that the cycle is repeated. You can also use Boolean variables to store the result of the validations.

The proposed code with do-while is as follows:

String toList="";
Boolean estaVacio = false;
Boolean contieneComa = false;

System.out.println("Escribe un String para convertirlo a List: ");

do
{
    toList = br.readLine();

    if (estaVacio = (toList == null || toList.trim().equals(""))
        System.out.println("El campo que ingreso esta vacio, vuelva a ingresarlo:");
    else if (contieneComa = toList.contains(","))
        CorreoUtilBuffereader1.Lista(toList);
    else
        System.out.println("Falta el caracter coma (,) por favor ingreselo:");

} while (estaVacio || !contieneComa);

This allows the user to first enter a text string and store it in the variable toList , then validate if it is empty or is null and store the result in the variable estaVacio , then validate whether the The entered string contains a comma character and the result will store it in the variable conteienComa , in both cases an error message is printed. In the case that everything is correct, it will execute the line CorreoUtilBuffereader1.Lista(toList); and it will leave the loop.

At the end of everything, the loop do-while evaluates the previous booleans and these determine if it is executed again. That is, after validating, if at the end of the loop the string is empty or does not contain a comma, then it is repeated again.

    
answered by 05.05.2018 в 05:54