Problems adding multiple elements to Arraylist

Asked

Viewed 41 times

-1

I’m practicing java and I’m having trouble printing the results inside Arraylist. Only shows the last typed result.

public class ArrayList {

    public static void main(String[] args) {

        Scanner teclado = new Scanner(System.in);
        List dados = new java.util.ArrayList<>();
        String resp;
        String nome;
        int idade;
        do {
            System.out.println("Nome: ");
            nome = teclado.next();
            System.out.println("Idade: ");
            idade = teclado.nextInt();
            System.out.println("Quer continuar ? ");
            resp = teclado.next();
            dados.add(nome);
            dados.add(idade);
        } while (!resp.equalsIgnoreCase("não"));

        for (int i = 0; i < dados.size(); i++) {
            System.out.println("Nome: " + nome + "\nIdade: " + idade);

        }

    }

}

3 answers

0

Good evening carlos... I saw very little of java , but from what I think you are printing only the name and age variable .. It is not directly accessing the array.

Do you agree with me that every time you enter the DO, you assign a new value, for name and age? for example first iteration: Name = "Peter" Age = 27

Second iteration: Name = 'Carlos' age = 20

The final value of the variable name and age will be Carlos and 20 ...

Try to access your array...

0

Your code needs a little tweaking. just set the list type, modified the change in the for to loop, after all it is always good to remember to close the resource.

 public static void main(String[] args) {
        Scanner teclado = new Scanner(System.in);
        List<String> dados = new ArrayList<String>(); // você precisa definir o tipo, isso pode ser uma instancia de obj
        String resp;
        String nome;
        String idade;
        do {
            System.out.println("Nome: ");
            nome = teclado.next();
            System.out.println("Idade: ");
            idade = teclado.next();
            System.out.println("Quer continuar ? ");
            resp = teclado.next();
            dados.add(nome);
            dados.add(idade);
        } while (!resp.equalsIgnoreCase("não"));

        for (int i = 0; i < dados.size(); i++) {
            System.out.println("Nome: " + dados.get(i) + "\nIdade: " + dados.get(i+1));
            i++;
        }
        teclado.close();
    }

another example when it comes to modern java would be to subistituite all this for by just:

dados.forEach(System.out::println);

it is good to remember that in the place where the data was placed it would be your specific class with its attributes, it would look much better. is the tip.

You almost got it right, you’re on the right track 'cause you tried.

0

If you change the line: List dados = new java.util.ArrayList<>();

To: ArrayList<string> dados = new java.util.ArrayList<>();

Also change the data type of "age" of int for String.

To access an element in an Arraylist use the method .get(), inside the parentheses puts the index reference (after the loop for, just replace your.out system with: System.out.println(dados.get(i));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.