fill as data an Array up to a specific value

Asked

Viewed 82 times

-3

I need to make a program that reads the keyboard names until the user type the word "order", and then print the typed names in the order they were typed.

So far, I’ve tried to resolve this issue used ArrayList, in this way:

public class Exercicios {

    public static void main(String[] args) {

        ArrayList<String> nomes = new ArrayList<>();

        while(!nomes.contains("fim")){

            for(int i = 0; i < nomes.size();i++){

                System.out.println("informes quantos dados desejar e digite 'fim' para finalizar");
                nomes.add(nomes.get(i));
                System.out.println(nomes.toString());

            }  
        }
    }
 }

Remembering that pro compiler, no error presented, but no result appears. I wonder what might be happening?

  • 2

    Post what you have already done, we will not do your task.

  • Please take a read, on this page, how to ask yourself.

  • Hello, thanks for the tips. Well, so far all I’ve written related to this issue is as follows. Remembering that pro compiler, no error presented, but no result appears. I wonder what might be going on, how you could help me with this problem?

2 answers

1

The problem is that you forgot to fill the ArrayList of names before the for. This line nomes.add(nomes.get(i)); doesn’t make much sense either.

Translate, you should take the for, read the value with something with the System.in or with the JOptionPane and check before printing. In logic it would be something like:

do {
   System.out.println("informes quantos dados desejar e digite 'fim' para finalizar");
   String nome = ... // aqui você lê o nome
   if (nome.equals("fim")) {
      break;
   } else {
      // adiciona na lista aqui
   }
} while(true);

Try it there, if you continue not succeeding, post your changes and how far you managed to get.

0

Your code basically doesn’t receive the data. I think that’s the confusion, because nomes.get() does not read data.

For a simple program, you can use the class Scanner, as in this other question.

Browser other questions tagged

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