fill an Array with data up to a specific value

-3

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

So far, I have tried to resolve this issue using ArrayList , like this:

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 has appeared, but no results appear. What could be happening?

    
asked by anonymous 09.10.2015 / 20:35

2 answers

1

The problem is that you forgot to fill in the ArrayList of names before the for. This nomes.add(nomes.get(i)); line also does not make much sense.

Translating, you should take for , read value with something with System.in or JOptionPane , and check before printing. In logic it would look 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 there, if you continue to fail, post your changes and how far you've gotten.

    
28.10.2015 / 20:59
0

Your code basically does not receive the data. I think this is the confusion because nomes.get() does not read data.

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

    
29.10.2015 / 07:29