2016-07-20 5 views
0

このコードに問題があります。それは不完全ですが、今私の目的は、メニューが動作するかどうかをテストすることです。基本的には、メニューを実装することです。無効な文字が入力された場合は、引き続き有効な選択を求めるプロンプトが表示されます。ヒント:他のオプションを実装する前にQuitを実装してください。私は間違って何をしているのか分かりません。私はそれを実行するとNoSuchElement in my code

import java.util.Scanner; 

public class Playlist 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String title = ""; 

     System.out.println("Enter playlist's title: "); 
     title = input.nextLine();   
     printMenu(title); 
    } 

    // Implement the printMenu() method. 
    // printMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. 
    // Each option is represented by a single character. 
    // Build and output the menu within the method. 

    public static void printMenu(String playlistTitle) 
    { 
     Scanner input = new Scanner(System.in); 
     boolean menu = true; 
     String option; 

     SongEntry songentry = new SongEntry(); 

     System.out.println(playlistTitle + " PLAYLIST MENU"); 
     System.out.println("a - Add song"); 
     System.out.println("d - Remove song"); 
     System.out.println("c - Change position of song"); 
     System.out.println("s - Output songs by specific artist"); 
     System.out.println("t - Output total time of playlist (in seconds)"); 
     System.out.println("o - Output full playlist"); 
     System.out.println("q - Quit"); 
     System.out.println("");   


     while(menu == true) 
     { 
      System.out.println("Choose an option: "); 

       switch(option = input.next()) 
       { 
        case "q": 

         menu = false; 

         break; 

        case "o": 

         System.out.println(playlistTitle + " - OUTPUT FULL PLAYLIST"); 

         break; 

        case "a": 

         System.out.println("ADD SONG"); 
         System.out.println("Enter song's unique ID: "); 
         System.out.println("Enter song's name: "); 
         System.out.println("Enter artist's name: "); 
         System.out.println("Enter song's length: "); 

         break; 

        case "d": 

         System.out.println("REMOVE SONG"); 
         System.out.println("Enter a song's unique ID: "); 
         System.out.println(" removed"); 

         break; 

        case "c": 

         System.out.println("CHANGE POSITION OF SONG"); 
         System.out.println("Enter a song's current position: "); 
         System.out.println("Enter a new position for song: "); 
         System.out.println(" moved to position "); 

         break; 

        case "s": 

         System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST"); 
         System.out.println("Enter artist's name: "); 

         break; 

        case "t": 

         System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)"); 
         System.out.println("Total time: seconds"); 

         break; 
       } 
      } 
     } 
} 

は、私が取得:java.utilのでjava.util.Scanner.throwFor(Scanner.java:907)でスレッド "メイン" java.util.NoSuchElementExceptionで

例外。 Scanner.next(Scanner.java:1416)at Playlist.printMenu(Playlist.java:45)at Playlist.main(Playlist.java:12)

ありがとうございました。

+0

'input.next()'を呼び出す前に、スキャナが 'input.hasNext()'によってバッファに何かを持っているかどうかチェックする必要があります。 –

+0

[No such Element Exception?](Nostackoverflow.com/questions/8032099/no-such-element-exception) –

+0

の重複の可能性があります。 '== true'をチェックしないでください。 – shmosel

答えて

0

あなたは、私が実際に私はprintMenuメソッドにスキャナを渡すために必要なことがわかっスイッチ

+0

実際には、スキャナを閉じると、基底のストリーム( 'System.in')も閉じ、他のスキャナは使用できなくなります。 1つのスキャナオブジェクトのみを使用し、必要に応じてそのオブジェクトを渡すことをお勧めします。しかし、実際の問題は 'next()'を呼び出す前にスキャナの状態をチェックしないことと関係していると思います。 –

0

を入力するときに開いて2台のスキャナを持って、あなたがメソッドを呼び出す前に、スキャナを閉じてください。それは今働く。皆さん、ありがとうございました!

import java.util.Scanner; 

public class Playlist 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     String title = ""; 

     System.out.println("Enter playlist's title: "); 
     title = input.nextLine();   
     printMenu(title, input); 
    } 

    // Implement the printMenu() method. 
    // printMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. 
    // Each option is represented by a single character. 
    // Build and output the menu within the method. 

    public static void printMenu(String playlistTitle, Scanner input) 
    { 

     boolean menu = true; 
     String option; 

     SongEntry songentry = new SongEntry(); 

     System.out.println(playlistTitle + " PLAYLIST MENU"); 
     System.out.println("a - Add song"); 
     System.out.println("d - Remove song"); 
     System.out.println("c - Change position of song"); 
     System.out.println("s - Output songs by specific artist"); 
     System.out.println("t - Output total time of playlist (in seconds)"); 
     System.out.println("o - Output full playlist"); 
     System.out.println("q - Quit"); 
     System.out.println("");   


     while(menu == true) 
     { 
      System.out.println("Choose an option: "); 

       switch(option = input.next()) 
       { 
        case "q": 

         menu = false; 

         break; 

        case "o": 

         System.out.println(playlistTitle + " - OUTPUT FULL PLAYLIST"); 

         break; 

        case "a": 

         System.out.println("ADD SONG"); 
         System.out.println("Enter song's unique ID: "); 
         System.out.println("Enter song's name: "); 
         System.out.println("Enter artist's name: "); 
         System.out.println("Enter song's length: "); 

         break; 

        case "d": 

         System.out.println("REMOVE SONG"); 
         System.out.println("Enter a song's unique ID: "); 
         System.out.println(" removed"); 

         break; 

        case "c": 

         System.out.println("CHANGE POSITION OF SONG"); 
         System.out.println("Enter a song's current position: "); 
         System.out.println("Enter a new position for song: "); 
         System.out.println(" moved to position "); 

         break; 

        case "s": 

         System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST"); 
         System.out.println("Enter artist's name: "); 

         break; 

        case "t": 

         System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)"); 
         System.out.println("Total time: seconds"); 

         break; 
       } 
      } 
     } 
}