2013-10-13 57 views
5

私は、ビルドして、ユーザに単語を入力するよう求めるJavaプロジェクトを実行しています。そして、 "done"が入力されると、プログラムはソートされた文字列リストを出力します。実行が失敗しました。java.util.NoSuchElementException

私はgradle -buildコマンドでプロジェクトをビルドすると正常にビルドされますが、実行しようとするとGradleは単語を入力するのに時間を許さず、次にGradle tell私は "行が見つかりませんでした"。ここで

は私のJavaプログラムで)私の機能を無効enter_words(の抽出物である:ここで

System.out.println("Enter a word"); 
    str=sc.nextLine(); 

は、私はシェルでGradleのrunコマンドを使用するときのGradleは私に言ったものです:

C:\Users\celine\Desktop\test2>gradle run 
:compileJava UP-TO-DATE 
:processResources UP-TO-DATE 
:classes UP-TO-DATE 
:runMain 
Enter a word // the program is running but I can't enter a word 
Exception in thread "main" java.util.NoSuchElementException: No line found 
     at java.util.Scanner.nextLine(Scanner.java:1585) 
     at d.Project1.enter_words(Project1.java:42) 
     at d.Project1.main(Project1.java:81) 
:runMain FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':runMain'. 
> Process 'command 'C:\Program Files\Java\jdk1.7.0_40\bin\java.exe'' finished wi 
th non-zero exit value 1 
ここで

は私のJavaプログラムです:

package com.gumgum.test; 

import java.util.Arrays; 
import java.util.Scanner; 

import org.apache.commons.lang.StringUtils; 

public class Project1 { 

    private int max; 
    private String[] userInputs; 

    // constructors 
    public Project1() { 
     max=100; 
     userInputs = new String[max]; 
    } 

    public Project1(int n) { 
     max=n; 
     userInputs = new String[max]; 
    } 

    //getter 
    String[] getUserInputs() { 
     return userInputs; 
    } 

    //setter: all words entered by the users are written in tab 
    public void setUserInputs(String[] tab, int position, String str) { 
     tab[position]=str; 
    } 

    //demande à l'utilisateur d'entrer un mot 
    public void enter_words() { 
     Scanner sc = new Scanner(System.in); 
     int countWords=0; 
     String str=""; 
     while ((countWords<3) || (str.equals("done")!=true)) { 

      System.out.println("Enter a word"); 
      str=sc.nextLine(); 

      if (StringUtils.isBlank(str) == true) 
        System.out.println("Please enter a valid word"); 
      else if (str.length()<=2) 
       System.out.println("Please enter at least 3 characters"); 

      if ((str.length()>=3) && (StringUtils.isBlank(str) != true)) { 
       switch(str) { 
        case "done" : 
         if (countWords<3) 
          System.out.println("Please enter at least three words"); 
         break; 
        default: 
         setUserInputs(this.userInputs,countWords,str); 
         countWords++; 
         break; 
       } 
      } 
     } 
    } 

    public void lexico_order(String[] tab) { 
     int i=0; 
     System.out.println(""); 
     while(tab[i]!=null) { 
      i++; 
     } 
     int k=0; 
     Arrays.sort(tab, k, i); 
     System.out.println("****** Sorted String List *******"); 
     for (int j=0;j<i;j++) 
      System.out.println(tab[j]); 
    } 

    public static void main(String[] args) { 
     Project1 p; 
     p = new Project1(); 
     p.enter_words(); 
     System.out.printf("ca marche"); 
     p.lexico_order(p.getUserInputs()); 
    } 

} 

答えて

3

追加:私のbuild.gradleに

run { 
standardInput = System.in 
} 

は私

をするために、この問題を解決
関連する問題