0

When I try to create the custom band array, I cannot input a value for position zero. This is what is printed:

"Enter the band name for the 0 position of the array: Enter the band name for the 1 position of the array:"

But, the rest of the input messages appear correctly and I can input a sentence. I tried using entry.next(); but I cannot have a value with space in it (such as Pink Floyd, it is read as 2 values). The rest of the loop appears correctly as:

Enter the band name for the 2 position of the array:

Enter the band name for the 3 position of the array:

etc..

What can I do to fix this? Thank you in advance!

Scanner entry = new Scanner (System.in);

    System.out.print("Enter number of bands: ");
    int slots = entry.nextInt();
    String[] bands = new String[slots];

    int x = 0;
    String ii;
    int i ,y = 1, pos;
    String temp = "0";
    boolean dupl = false;

    System.out.println();

This is the part with the faulty code.

    while (x < slots) {
        System.out.print("Enter the band name for the " + x + " position of the array: ");

        ii = entry.nextLine();

        bands[x] = ii;
        x++;
    }
  • After `int slots = entry.nextInt();`, add `entry.nextLine();`. – Andy Turner Dec 13 '18 at 20:12
  • @AndyTurner thanks! But why does that work? What does the "entry.nextLine();" do on its own? – Αλεξ Μαρκ Dec 13 '18 at 20:17
  • 1
    Did you visit duplicate question (it should be linked at top of your question)? It contains many answers explaining that subject like [this one](https://stackoverflow.com/a/39949330) (self promotion). If you will still have question about it *after* reading those answers, consider creating separate question in what you point out what *specifically* is confusing you about it. – Pshemo Dec 13 '18 at 23:12

0 Answers0