2017-06-02 4 views
2

私はJavaが初めてです。私はテキストのアスペクト・センチメント分析を実装するコードを使って作業しています。私はコードが動作するはずですが、どういうわけか私は次のエラーが発生し続けることを知っています。ここでJavaがForステートメントをスキップするのはなぜですか?

----jGRASP exec: java -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,suspend=y,server=y sto2.STO2Core 
----jGRASP: connected to debugger. 
Exception in thread "main" java.lang.Exception: Alpha should be specified as a positive real number. 

----jGRASP debugger: run in canvas ended 
    at sto2.STO2Core.main(STO2Core.java:114) 

はラインを実行するように見える場合、私は場合や、他のプログラムのどれを実行しない場合、私はエラーに

public static void main(String [] args) throws Exception { 
    int numTopics = 10; 
    int numIterations = 100; 
    int numSenti = 2; 
    int numThreads = 1; 
    String inputDir = null; 
    String outputDir = null; 
    String dicDir = null; 
    double alpha = -1; 
    double [] betas = null; 
    double [] gammas = null; 
    String [] betasStr = null; 
    String [] gammasStr = null; 
    boolean randomInit = false; 

    String sentiFilePrefix = "SentiWords-"; 
    String wordListFileName = "WordList.txt"; 
    String docListFileName = "DocumentList.txt"; 
    String wordDocFileName = "BagOfSentences.txt"; 


    for (int i = 0; i < args.length/2; i++) { 
     String option = args[2*i]; 
     String value = args[2*i+1]; 
     if (option.equals("-t")) numTopics = Integer.valueOf(30); 
     else if (option.equals("-s")) numSenti = Integer.valueOf(2); 
     else if (option.equals("-i")) numIterations = Integer.valueOf(1000); 
     else if (option.equals("-th")) numThreads = Integer.valueOf(3); 
     else if (option.equals("-d")) inputDir = value.replaceAll("T:/Summer 2017/ASUM/ASUM/Test data", "/").replaceAll("/$", ""); 
     else if (option.equals("-o")) outputDir = value.replaceAll("T:/Summer 2017/ASUM/ASUM/Output", "/").replaceAll("/$", ""); 
     else if (option.equals("-dic")) dicDir = value.replaceAll("T:/Summer 2017/ASUM/ASUM/Test data", "/").replaceAll("/$", ""); 
     else if (option.equals("-a")) alpha = Double.valueOf(0.1); 
     else if (option.equals("-b")) betasStr = value.split("0.001/0.1/0"); 
     else if (option.equals("-g")) gammasStr = value.split("1/1"); 
     else if (option.equals("-r")) randomInit = value.toLowerCase().equals("true")?true:false; 
    } 
    if (inputDir == null) inputDir = "."; 
    if (outputDir == null) outputDir = new String(inputDir); 
    if (dicDir == null) dicDir = new String(inputDir); 

    // Exceptions 
    if (!new File(inputDir).exists()) throw new Exception("There's no such an input directory as " + inputDir); 
    if (!new File(outputDir).exists()) throw new Exception("There's no such an output directory as " + outputDir); 
    if (!new File(dicDir).exists()) throw new Exception("Tehre's no such a dictionary directory as " + dicDir); 

    if (alpha <= 0) throw new Exception("Alpha should be specified as a positive real number."); 

を得るラインゴマアップ私の主な機能のコードです。問題の原因は何か? ありがとうございました!

+1

プログラムにコマンドライン引数を渡していますか? – Eran

+0

はい、opは次のようなことをしています:*** args [2 * i]; *** argsが与えられていないとクラッシュします –

+3

@ΦXocę笑Пepeúpaツいいえ、argsが与えられていなければループは入力されません。 – Eran

答えて

2

[] argsは決してnullにはなりませんが、アプリケーションの実行中に引数を渡さないと空の配列になります。

ことを覚えておいて、for文のこのバージョンを使用して:初期化式は、ループを初期化

  • 。ループが始まると、一度実行されます。
  • 終了式がfalseに評価されると、ループは終了します。
  • ループを通る各繰り返しの後にインクリメント式が呼び出されます。この式が値を増減することは完全に許容可能です。

あなたの条件は(args.lenghtが0で、iは0 0 0以上である)最初の反復の前に偽です - アプリケーションはループに入りません。あなたがその例外を取得どこ

enter image description here

0

argsがない場合、args.length/2はゼロです。つまり、forループの条件は自動的にfalseになるため、ループ本体は一度も実行されません。

0

上記のコードの最後の行です。オプション[x] = -aの場合にのみ、aplphaを0.1に割り当てます。各反復でiの値が倍増しているので、あなたのコードはコード行を実行していない可能性があります。else if(option.equals( "-a"))alpha = Double.valueOf(0.1);

関連する問題