2017-05-19 3 views
0

に渡さ:は方法

Beginner in Java - Using Parameters and Scanner

問題の練習サイトは、クラスを作成し、使用して書き込みメソッドを呼び出します次の呼び出し: inputBirthday(新しいスキャナ( "8 \ nMay \ n1981 \ n"));

私はそれがうまくいかない理由を知りたいのです。私のメソッドのコードはこれです:私はこのエラーを得続ける

public static void inputBirthday(Scanner scan) { 
    System.out.print("On what day of the month were you born? "); 
    int monInt = scan.nextInt(); 
    System.out.print("What is the name of the month in which you were 
    born? "); 
    String monStr = scan.nextLine(); 
    System.out.print("During what year were you born? "); 
    int year = scan.nextInt(); 
    scan.close(); 
    System.out.println("You were born on " + monStr + ", " + monInt + year 
    + ". You're mighty old!"); 
} 

「はNoSuchElementException:入力ライン3token近く、int型として解釈することはできません "メイ」

右この行の後私のコード:

int year = s.nextInt(); 

アイデアはありますか?ありがとう!

+1

[読み取り文字列next()とnextLine()Java]の複製が可能です(http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java) – Dukeling

+0

の代わりに 'next'を使用してください月を読むための 'nextLine' – Dukeling

+0

エラーをコードとしてフォーマットしてください。これは混乱のように見えます。 –

答えて

0

解析"8\nMay\n1981\n "それで問題はnextLine()は最初の行ではなく、メイズ行の終わりを読み取っていることです。

1

あなたは月の日付を入力すると、あなたが生まれて、Enterキーを押しますされている、すなわち

System.out.print("On what day of the month were you born? "); 
    int monInt = scan.nextInt(); 

あなたは彼らが番号を入力することが期待されています。 13を入力してEnterを押すと、enterを押したので、13\nと入力したことがコンピュータに表示されます。 nextInt()への呼び出しは13となりますが、\nのままにしてください。 nextLine()が動作する方法は、\nの次の出現まで読み取ることです。つまり、\nが行を終了するために使用されます。この問題は、次に System.out.print("During what year were you born? "); int year = scan.nextInt(); と呼んでいますが、まだ生まれた月の文字列表記を入力することを期待しています。したがって、Stringを入力すると、nextInt()の呼び出しは整数ではないため、整数として解析できません。これはStringです。 これを修正するには2つの方法があります。入力バッファから\nを取り除くために初めてnextInt()を呼び出した直後にscan.nextLine()を呼び出すか、またはscanを使うことができます。 scan.nextLine()の代わりにnext()を呼び出して、その月のString表現を格納します。 next()は(通常)次のスペースまで読み込まれるので、入力バッファにまだ存在する\nは問題を起こさないでしょう。あなたのコードの機能バージョン、次のことになる -

public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("On what day of the month were you born? "); 
     int monInt = input.nextInt(); 
     System.out.print("What is the name of the month in which you were born? "); 
     String monStr = input.next(); 
     System.out.print("During what year were you born? "); 
     int year = input.nextInt(); 
     input.close(); 
     System.out.println("You were born on " + monStr + ", " + monInt + year 
       + ". You're mighty old!"); 
    } 

と、このプログラムからの出力例は、あなたがまだ出力にそれを得るために書式設定テキストを変更する必要が

On what day of the month were you born? 13 
What is the name of the month in which you were born? October 
During what year were you born? 1943 
You were born on October, 131943. You're mighty old! 

注だろうあなたが望むのとまったく同じですが、あなたが以前に抱えていた問題は修正されています。

int monInt = input.nextInt(); // Reads 8 
    System.out.print("What is the name of the month in which you were born? "); 
    String monStr = input.nextLine(); // Reads to end of line, aka '\n' 
    System.out.print("During what year were you born? "); 
    int year = input.nextInt(); // reads next token as int, which is "May"