Save user input in a loop [closed]

-1

I want to get the list of the variable 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 .

    
asked by anonymous 23.09.2016 / 14:09

1 answer

2
  

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

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

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

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 this:

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

Or so:

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

See DEMO

    
23.09.2016 / 18:34