1

I am only in my second semester of java so there is probably a very simple solution to my problem, but I have been pulling my hair our for hours because of this. The following code is being used to collect user input and store instance's info in an array.

There isn't an opportunity to input anything for the album name. The output is as follows.

Enter 5 songs

Title: title
Author: author1
Interpreter: int1
Year released: 2000
Album: File name: //Why doesn't this collect input and place File name: on the next line.

If anyone could point me in the right direction I would greatly appreciate it. I included the two methods below in case they have something to do with my problems. Thanks for any help you can provide.

    public static void main(String[] args){

    Scanner kybd = new Scanner(System.in);

    Song[] songTest = new Song[5];

    System.out.println("Enter 5 songs\n");


    //TEST
    for(Song x:songTest)
    {
        x = new Song();
        System.out.print("Title: ");
        x.setTitle(kybd.nextLine());
        System.out.print("Author: ");
        x.setAuthor(kybd.nextLine());
        System.out.print("Interpreter: ");
        x.setInterpreter(kybd.nextLine());
        System.out.print("Year released: ");
        x.setYearReleased(kybd.nextInt());
        System.out.print("Album: ");
        x.setAlbum(kybd.nextLine()); //this doesn't allow for input.  it prints "File name:" and skips the user input for an album name.  Also, when I comment out Year released, the problem goes away.
        System.out.print("File name: ");
        x.setFileName(kybd.nextLine());
        System.out.print(x);
        System.out.println();
    }


public void setYearReleased(int y)
{
    if (y>0 && y<2013)
        this.yearReleased = y;
    else
    {
        System.out.print ("This song is not that old");
        this.yearReleased = -5;
    }
}

    public void setAlbum(String a)
{
    this.album = a;
}
1
  • 1
    I love how setting the release year to 2014 results in 'This song is not that old' being printed on standard output. Commented Sep 23, 2012 at 19:09

2 Answers 2

3

You need to do a kybd.nextLine() after kybd.nextInt() so that the following kybd.nextLine() does not capture the newline used when pressing enter for the nextInt().

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help. There are some very frustrating rookie mistakes that I run into from time to time. Also I removed the restraint that a song needs to be <2013. Also, a good bit of constructive criticism.
2
x.setYearReleased(kybd.nextInt());

does not do anything with the end of the line character and will remain on the line giving the behavior you see. A quick fix could be to just add another ktbd.nextLine(); after this to skip anything else on the line after the next int.

System.out.print("Year released: ");
x.setYearReleased(kybd.nextInt());
kybd.nextLine()
System.out.print("Album: ");
x.setAlbum(kybd.nextLine());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.