2016-04-09 13 views
2

私はプログラミングの初心者です。この問題は常に私に直面します 私はプログラムを実行すると、Java内部で文字列入力を無視します どうしましたか?Javaは文字列を無視します

import java.util.Scanner; 

public class JavaApplication { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     System.out.print("------ FEEDBACK/COMPLAINT ------\n" 
       + "-------------------------------------\n" 
       + "| 1: Submit Feedback |\n" 
       + "| 2: Submit Complaint |\n" 
       + "| 3: Previous Menu |\n" 
       + "-----------------------------------\n" 
       + "> Please enter the choice: "); 
     int feedorcomw = input.nextInt(); 

     if (feedorcomw == 1) { 
      String name; 
      System.out.print("> Enter your name (first and last): "); 
      name = input.nextLine(); 
      System.out.println(""); 
      System.out.print("> Enter your mobile (##-###-####): "); 
      int num = input.nextInt(); 

     } 

    } 
} 
+0

どのようなエラーが表示されますか? – Andrew

+2

これはあなたの問題に答えるかもしれません。http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – RubioRic

+0

あなたの編集はあまり意味がありません - なぜですかフォーマットを修正するだけでなく、完全な例を最小限にする – Flexo

答えて

1

あなたはスキャナ#nextInt方法は、あなたの入力の最後に改行文字を消費しないので、その改行はスキャナ#nextLineの次の呼び出し

で消費されているという事実をommittingていますその後input.nextLine();を追加してみてください、すべてが例

正常に動作します:

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    System.out.print("------ FEEDBACK/COMPLAINT ------\n" 
      + "-------------------------------------\n" 
      + "| 1: Submit Feedback |\n" 
      + "| 2: Submit Complaint |\n" 
      + "| 3: Previous Menu |\n" 
      + "-----------------------------------\n" 
      + "> Please enter the choice: "); 
    int feedorcomw = input.nextInt(); 

    input.nextLine(); 
    if (feedorcomw == 1) { 
     String name; 
     System.out.print("> Enter your name (first and last): "); 
     name = input.nextLine(); 
     System.out.println(""); 
     System.out.print("> Enter your mobile (##-###-####): "); 
     int num = input.nextInt(); 

    } 

} 
関連する問題