Java不断跳过我的扫描仪对象

时间:2016-06-05 08:39:34

标签: java class object setter getter

这对我来说非常令人费解。我试图了解如何解决Java的这个问题而不是认识到我的" setTitle"方法存在于第一首" Song之后。"这是一个音乐应用程序。它还有另外两个类。非常感谢所有的帮助。

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());
}
}

编译器做了一些有趣的事情,比如跳过某些字段。请参阅下面的编译器输出:

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)

修改 添加了宋课。

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";
    }
}

1 个答案:

答案 0 :(得分:1)

尝试在每次拨打' nextInt()'

后将其设置为
keyboard.nextLine() 

基本上它会跳过一个输入,因为nextInt不会捕获" \ n"。 您可以看到更好的解释here