Hey guys, I am trying to make a program that allows you to input music artist into an array. The array row length is dependent upon a number entered by the user. There are only three column. I am having trouble with a line inside the for loop printing twice in a row before recieving input for the array. However if I change the " int songs=keyboard.nextInt " to a " final songs=3 or any other number, the loop processes correctly. It seems to be an issue with the keyboard.nextint not reading correctly in the array I assume. Here is part of my code.

public class Music 
{
    public static void main(String[] args)
    {               
        System.out.print("You may enter up to 20 songs. "+
                "How many songs would you like to enter?");

        Scanner keyboard=new Scanner(System.in);
        int songInfo=3;
        int songs=keyboard.nextInt();
        String[][] music=new String[songs][songInfo];

        System.out.println("Please, enter song title, artist, and album title.");
        for(int row=0; row<songs; row++)
        {       
            for(int col=0; col<songInfo; col++)
            {
                System.out.print("Enter inforomation.");
                music[row][col]=keyboard.nextLine();                
                }
            System.out.println();
        }

The loop prints Enter Information. Enter Information. before it asks for an input for the first Enter Information. Any help would be appreciated. Just looking for a push in the right direction.

Recommended Answers

All 5 Replies

Can you post the console that shows what the program does when you execute it. Add comments to the posting to show where the problem is.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

You could be having problems with the way the Scanner class works. Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only "A" is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

I'm using Eclipse to write this in.
When I run the program here is what is displayed.

You may enter up to 20 songs. How many songs would you like to enter?2 <--- First line prints and I entered 2

Please, enter song title, artist, and album title.

Enter inforomation.Enter inforomation. <---- After inputing two and hitting enter the next two lines printed.

It should stop after the first Enter Information. And let me input the song title and press enter. Then display the next Enter information.

You probably need to clear Scanner's buffer by calling nextLine() after calling nextInt().

I'm not sure I follow what you mean. I am pretty new to Java, although I know VB.net fairly well. Forgive me, I know this has to be a simple issue.

--Edit-- Ah I missed the second part of your first post. I will try some things.

Excellent sir, I thank you. I got it fixed. I just created an extra variable and assigned it keyboard.nextLine and it took care of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.