1

I have following class

import java.util.Scanner;

public class Album{

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        System.out.println("How many songs do your CD contain?");
        int songs = sc.nextInt();

        String[] songNames = new String[songs];

        for (int i = 0; i < songs; i++) {
            System.out.println("Please enter song nr " + (i+1) + ": ");
            songNames[i] = sc.nextLine();
            // What is wrong here? (See result for this line of code)
            // It is working when I use "sc.next();"
            // but then I can't type a song with 2 or more words.
            // Takes every word for a new song name. 
        }

        System.out.println();
        System.out.println("Your CD contains:");
        System.out.println("=================");
        System.out.println();

        for (int i = 0; i < songNames.length; i++) {
            System.out.println("Song nr " + (i+1) + ": " + songNames[i]);
        }
    }
}

I can't type song name nr 1 because it Always shows first two together.

Like this if I type 3:

How many songs do your CD contain?
3
Please enter song nr 1:
Please enter song nr 2: 
ljgw
  • 2,751
  • 1
  • 20
  • 39
gozluklu_marti
  • 79
  • 1
  • 7
  • 26
  • I edited your question to give it a somewhat better title. Of course, your old title gives people a challenge to do a little code-detective work to find the problem in your code, and it served it's purpose well with 5 answers, but the previous _What is wrong with my code? Check my comment lines_ could have fitted arbitrarily many questions. – ljgw Aug 27 '14 at 11:33

5 Answers5

3

Change

int songs = sc.nextInt();

to:

int songs = Integer.parseInt(sc.nextLine().trim());

and it will work fine.

You should not mix usages of nextInt with nextLine.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

add sc.nextLine(); after int songs = sc.nextInt();

Once you enter a number and read it using a scanner ( as a number) using sc.nextInt();, the newline character will be present in the input stream which will be read when you do sc.nextLine(). So,to skip(over) it, you need call sc.nextLine() after sc.nextInt();

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Add a sc.nextLine() after sc.nextInt() and your code works fine.

The reason is the end of line after you type the nomber of songs.

Jens
  • 67,715
  • 15
  • 98
  • 113
0

Either use nextInt or nextLine, I would opt for nextLine:

import java.util.Scanner;

public class Album{

  public static void main(String[] args){

    Scanner sc = new Scanner(System.in);
    System.out.println("How many songs do your CD contain?");
    int songs = Integer.parseInt(sc.nextLine()); // instead of nextInt()

    String[] songNames = new String[songs];

    for (int i = 0; i < songs; i++) {
        System.out.println("Please enter song nr " + (i+1) + ": ");
        songNames[i] = sc.nextLine();
        // What is wrong here? (See result for this line of code)
        // It is working when I use "sc.next();"
        // but then I can't type a song with 2 or more words.
        // Takes every word for a new song name. 
    }

    System.out.println();
    System.out.println("Your CD contains:");
    System.out.println("=================");
    System.out.println();

    for (int i = 0; i < songNames.length; i++) {
        System.out.println("Song nr " + (i+1) + ": " + songNames[i]);
    }
  }
}
tmarwen
  • 15,750
  • 5
  • 43
  • 62
0

put a sc.nextLine(); after int songs = sc.nextInt();

DeiAndrei
  • 947
  • 6
  • 16