为什么Java会跳过For语句?

时间:2017-06-02 18:48:01

标签: java for-loop if-statement compiler-errors

我是Java的新手。我正在使用实现文本方面情感分析的代码。我知道代码应该可以工作,但不知怎的,我不断收到以下错误。

  ----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.");

当我执行程序时,如果行似乎没有if或else。可能是问题的根源是什么? 谢谢!

3 个答案:

答案 0 :(得分:2)

[] args永远不能为null,但是如果在运行应用程序时没有传递任何参数,那么它将是空数组。

使用此版本的for语句时,请记住:

  • 初始化表达式初始化循环;当循环开始时,它被执行一次。
  • 当终止表达式的计算结果为false时,循环终止。
  • 每次迭代循环后调用increment表达式;这个表达式增加或减少一个值是完全可以接受的。

如果在第一次迭代之前条件为假(args.lenght为0且i为0. 0不小于0) - 应用程序将不会进入循环。

enter image description here

答案 1 :(得分:0)

如果没有args,则args.length/2为零,这意味着for循环中的条件将自动为false,因此循环体不会执行一次。

答案 2 :(得分:0)

上面代码中的最后一行是获取该异常的地方。仅当选项[x] = -a时,才将aplpha指定为0.1。由于你在每次迭代中将i的值加倍,你的代码可能不会执行其他代码行(option.equals(&#34; -a&#34;))alpha = Double.valueOf(0.1);

相关问题