0
import java.util.Scanner;

public class EngineStorage {

    Engine[] engines = new Engine[3];

    Scanner scanner = new Scanner(System.in);

    public void fillArray(Engine[] engines) {
        String name;
        double vol;

        for (int i = 0; i < engines.length; i++) {

            System.out.println("name");
            name = scanner.nextLine();
            System.out.println("volume");
            vol = scanner.nextDouble();
        }
    }
}

when "i" is 0 it works fine but then loop is avoiding "name" variable when "i" is 1 and 2. The result is:

name
e32
volume   
2,5


name
volume
3


name
volume
3


Process finished with exit code 0
maloomeister
  • 2,461
  • 1
  • 12
  • 21
Daniel K
  • 49
  • 2

1 Answers1

0

nextDouble() does not skip to the next line, so the next time nextLine() is ran, it will read what is left on that line, which is nothing. You can consume the rest of this line with a blank newline like

for (int i = 0; i < engines.length; i++) {

        System.out.println("name");
        name = scanner.nextLine();
        System.out.println("volume");
        vol = scanner.nextDouble();
        scanner.nextLine(); //skip to next line
    }
Ryan Millares
  • 525
  • 4
  • 16