Search code examples
javalistarraylistjava.util.scanner

problem scan variable to add to arraylist


Good morning, I'm learning Java, I had a problem and I don't know what the solution is... I want to add a certain amount of data (name, address, money) to an arraylist, I do a for loop for this and when executing the Variable reading is not done correctly. When I use nextLine to read variable 0, the program jumps to 1, so I don't enter the amount of information I want Now, when I use next() this doesn't happen, but when I want to enter a space-separated name as data, the same jump is done as if I were using nextLine... does anyone have any recommendations? arrays

my code is not finished since I can't go from here

public static void main(String\[\] args) {
    Scanner sc = new Scanner(System.in);
    List <Cliente> lista = new ArrayList<Cliente>(); //declaracion y asignacion del ArrayList
    Cliente clie = new Cliente();

    System.out.println("De la cantidad de cientes a ingresar: ");
    int n = sc.nextInt();
    
    for (int i = 0; i < n; i ++) {
        //lista.add(new Cliente("nico","ramos ",1,25000));
        System.out.println("Al cliente se le asigno un numero de cliente");
        clie.setCodigoCliente(i);
        System.out.println("Ingrese el nombre del cliente " +i);
        String nom;
        clie.setNombre(nom = sc.nextLine());
        System.out.println("Ingrese la direccion del cliente "+i);
        String direccion;
        clie.setDireccion(direccion = sc.nextLine());
    }
}
public class Cliente {

    int codCliente;
    String nombre;
    String direccion;
    double credito;

    public Cliente() { }

    public Cliente(String nombre, String direccion, double credito) {
        // this.codCliente = codCliente;
        this.nombre = nombre;
        this.direccion = direccion;
        this.credito = credito;
    }

    // Getters and setters omitted for brevity

}

Solution

  • Try this code:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    class ClientTest {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    
        List<Cliente> lista = new ArrayList<Cliente>(); //declaracion y asignacion del ArrayList Cliente clie = new Cliente();
        System.out.println("Enter how many clients will be added:");
        int n = sc.nextInt(); // if you will type here '2 abc' then
    //n will be '2' and nextLine will be ' abc' that's why 'sc = new Scanner(System.in)' is used to reset the scanner
    
        sc = new Scanner(System.in);
        for (int i = 0; i < n; i++) {
    
            Cliente cliente = new Cliente();
            cliente.setCodigoCliente(i);
            System.out.println("Enter number of client# " + i);
            cliente.setNombre(sc.nextLine());
            System.out.println("Enter direccion of client: " + i);
            cliente.setDireccion(sc.nextLine());
            lista.add(cliente);
        }
        System.out.println(lista);
    }
    
    }
    
    class Cliente {
    
    int codCliente;
    String nombre;
    String direccion;
    double credito;
    
    public Cliente() {
    
    }
    
    public Cliente(String nombre, String direccion, double credito) {
        // this.codCliente = codCliente;
        this.nombre = nombre;
        this.direccion = direccion;
        this.credito = credito;
    }
    
    @Override
    public String toString() {
        return "Cliente{" +
                "codCliente=" + codCliente +
                ", nombre='" + nombre + '\'' +
                ", direccion='" + direccion + '\'' +
                ", credito=" + credito +
                '}';
    }
    
    public void setCodigoCliente(int codCliente) {
        this.codCliente = codCliente;
    }
    
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }
    
    public void setCredito(double credito) {
        this.credito = credito;
    }
    
    public int getCodCliente() {
        return codCliente;
    }
    
    public String getNombre() {
        return nombre;
    }
    
    public String getDireccion() {
        return direccion;
    }
    
    public double getCredito() {
        return credito;
    }
    }