Problem with do while loop in JAVA [duplicated]

0

I have the following dive while:

ArrayList<String> nombreP = new ArrayList();
ArrayList cantidadP = new ArrayList();
do{
        System.out.println("Producto?: ");
        nombreP.add(i, sc.nextLine());

        if(nombreP.get(i).equals("")){
            System.out.println("Compra terminada");
            break;
        }
        else{
            System.out.println("Unidades?: ");
            cantidadP.add(i, sc.nextInt());
        }
        i++;
    }while(!(nombreP.get(i-1).equals("")));

The problem is that it runs once and when it goes through the second run, does Product appear? Purchase finished. I want to assume that you are assigning the "intro" of Units? at the entrance of the second execution, and that's why it enters the if and leaves with the break, but I do not know how to fix it. Thanks.

    
asked by Mario Hernandez 16.12.2017 в 00:00
source

2 answers

0

I made some slight modifications to your idea. But here I have your answers in a simple way.

Map<Integer, Integer> productos = new HashMap<>(); // tabla de productos
  Scanner sc = new Scanner(System.in);
  String idProducto = "";
  String cantidad  = "";
  int i = 0;

while(true){ //Mientras halla espacio en el registro
    System.out.println("Producto?: ");
    idProducto = sc.nextLine(); // se guarda temporalmente la id del producto

    System.out.println("Unidades?: ");
    cantidad = sc.nextLine(); // se guarda la cantidad temporalmente

    if(idProducto.length() == 0 || cantidad.length() == 0) // evaluando que se no se imcumpla los requisitos
        break; // se termina el ciclo
    else { // en caso de que todo este bien
        productos.put(Integer.valueOf(idProducto), Integer.valueOf(cantidad));  // se almacena definitivamente los parametros
    }

    i++;
}

// Imprimimos el Map con un Iterador
Iterator it = productos.keySet().iterator();
while(it.hasNext()){
  Integer key = (Integer) it.next();
  System.out.println("id: " + key + " -> cantidad: " + productos.get(key));
}
    
answered by 16.12.2017 в 13:36
0

I made this solution:

    Scanner sc = new Scanner(System.in);
    Map<String, Integer> productos = new HashMap<>();
    String nombre;
    int cantidad = 0;

    do {
        System.out.println("Producto?: ");
        nombre = sc.nextLine();

        if(nombre.equals("")) {
            System.out.println("Compra terminada.");
        } else {
            System.out.println("Unidades?: ");
            cantidad = Integer.valueOf(sc.nextLine());
            productos.put(nombre, cantidad);
        }
    } while(!nombre.equals(""));

    Iterator n = productos.keySet().iterator();
    while(n.hasNext()) {
        String key = (String) n.next();
        System.out.println("Nombre: " + key + " -> Cantidad: " + productos.get(key));
    }

The best thing is that you always use nextLine () and cast it as the other ways of capturing the data do not capture the last character.

    
answered by 16.12.2017 в 21:28