0

This is very puzzling to me. I am trying to understand how to fix this problem of Java not recognizing that my "setTitle" method exists after the first "Song." This is a music application program. It has two other classes. All help would be very much appreciated.

import java.util.*;
public class MusicApp 

{
     static Scanner keyboard = new Scanner(System.in);
     static Song s1 = new Song();
     static Song s2 = new Song();
     static Song s3 = new Song();
     static Album a1 = new Album(); 

public static Song song1(Song s1)
{
    System.out.println("Song One");
    System.out.println("Enter the title of a song: ");
    s1.setTitle(keyboard.nextLine());
    System.out.println("Enter the artist's name: ");
    s1.setArtist(keyboard.nextLine());
    System.out.println("Enter the length of the song in minutes: ");
    s1.setMinutes(keyboard.nextInt());
    a1.add(s1);

    return s1; 
}
public static Song song2(Song s2)
{
    System.out.println("Song Two");
    System.out.println("Enter the title of a song: ");
    s2.setTitle(keyboard.nextLine());
    System.out.println("Enter the artist's name: ");
    s2.setArtist(keyboard.nextLine());
    System.out.println("Enter the length of the song in minutes: ");
    s2.setMinutes(keyboard.nextInt());
    a1.add(s2);

    return s2;
}
public static Song song3(Song s3)
{
    System.out.println("Song Two");
    System.out.println("Enter the title of a song: ");
    s3.setTitle(keyboard.nextLine());
    System.out.println("Enter the artist's name: ");
    s3.setArtist(keyboard.nextLine());
    System.out.println("Enter the length of the song in minutes: ");
    s3.setMinutes(keyboard.nextInt());
    a1.add(s3);

    return s3;
}
public static void main(String[] args) 
{
    song1(s1);
    System.out.println("");
    song2(s2);
    System.out.println("");
    song3(s3);

    System.out.println("Enter the title of a song in the album: ");
    Song songInput = a1.getTitle(keyboard.nextLine());
    System.out.println(songInput);
    System.out.println(a1.toString());
}
}

Compiler does some funny stuff like skip certain fields. See compiler output below:

Song One
Enter the title of a song: 
Three Little Birds
Enter the artist's name: 
Bob Marley
Enter the length of the song in minutes: 
5

Song Two
Enter the title of a song: 
Enter the artist's name: 
Very Best
Enter the length of the song in minutes: 
Ash 
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at MusicApp.song2(MusicApp.java:42)
    at MusicApp.main(MusicApp.java:64)

EDIT: Added Song class.

public class Song
{
    private String artist = "";
    private String title = "";
    private int minutes = 0;

    public Song()
    {
    }

    public String getArtist()
    {
        return artist;
    }

    public void setArtist(String singer)
    {
        artist = singer;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String name)
    {
        title = name;
    }

    public int getMinutes()
    {
        return minutes;
    }

    public void setMinutes (int mins)
    {
        minutes = mins;
    }

    public String toString()
    {
        return getTitle() + " by " + getArtist() + " is " + minutes + " minutes long";
    }
}
Computer
  • 132
  • 11

1 Answers1

1

Try to put this after every call to 'nextInt()'

keyboard.nextLine() 

Basically it's skips an input ,because the nextInt wouldn't catch the "\n". You can see a better explanation here

Community
  • 1
  • 1
Dima T.
  • 78
  • 5