2016-06-05 10 views
0

이것은 매우 당황 스럽습니다. Java의이 문제를 해결하는 방법을 이해하려고 노력하면서 첫 번째 "Song"이후에 "setTitle"메서드가 있다는 것을 인식하지 못했습니다. 이것은 음악 응용 프로그램입니다. 두 개의 다른 클래스가 있습니다. 모든 도움은 대단히 감사하겠습니다.Java가 내 스캐너 개체 위로 건너 뛰기를 계속합니다.

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) 

편집 : 추가 된 노래 클래스 아래 컴파일러 출력을 참조하십시오. nextInt는 "\ n을"을 잡을 것이기 때문에

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

Song 클래스를 게시 할 수 있습니까? 당신의'setTitle' 메쏘드에 대해 당신이하고있는 것을보고 싶습니다. –

+0

최근에 편집 한 – Computer

답변

1

봅니다,

keyboard.nextLine() 

기본적으로 그것의는 입력을 건너 뜁니다 ') nextInt ('모든 호출 한 후이를 넣어. 더 나은 설명을 볼 수 있습니다. here

+0

디마 (Dima)에서 Song 클래스를 추가했습니다. 그게 내 문제를 확실하게 해결했습니다. 감사합니다. – Computer