Save user input to a loop

Asked

Viewed 225 times

-1

I want to get the variable list numeros, generated by the following loop:

Scanner kb = new Scanner (System.in);
double [] numeros= new double[10];
for (int i = 0; i < numeros.length; i++)
{
    System.out.println("o proximo numero");

    numeros[i] = kb.nextDouble();
}

My question is how to store the generated list of this array.

  • 3

    And what is the doubt?

  • My question is how to store the generated list of this array.

  • It would be interesting to edit the question, because it is not in the format of a.

  • @cambine I edited your question, she’s a little confused, you can edit it or revert to previous state.

1 answer

2


My question is how to store the generated list of this array.

On the line numeros[i] = ... If I understand correctly, you already do this, keep in the array what is typed in the entry that can be read as a double.

As a suggestion, you can use Scanner.html#hasNextDouble before to check if the typed content can be interpreted as a value double:

Scanner kb = new Scanner (System.in);
double [] numeros = new double [10];

for (int i = 0; i < numeros.length; i++) {
    System.out.println("Digite o próximo número: ");

    if(kb.hasNextDouble()) { 
        numeros[i] = kb.nextDouble();
    }
}

To display the contents of array, you can do so:

System.out.println(Arrays.toString(numeros));

Or so:

for (double numero: numeros) {
    System.out.println(numero);
}

See DEMO

Browser other questions tagged

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