【问题标题】:Why is Java skipping the For statement?为什么 Java 会跳过 For 语句?
【发布时间】:2017-06-02 18:48:00
【问题描述】:

我是 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)

这是我的 main 函数的代码,直到出现错误的那一行

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 if 行似乎都没有运行。问题的根源是什么? 谢谢!

【问题讨论】:

  • 您是否将任何命令行参数传递给您的程序?
  • 是的,op 正在做类似的事情: args[2*i]; 如果没有给出 args 将会崩溃
  • @ΦXocę웃Пepeúpaツ 不,如果没有给出参数,则不会进入循环。
  • 好眼光@Eran,我错过了这部分 args.length/2 :)

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


【解决方案1】:

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

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

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

当您的条件在第一次迭代之前为假时(args.lenght 为 0,i 为 0。0 不小于 0)- 应用程序不会进入循环。

【讨论】:

    【解决方案2】:

    如果没有args,那么args.length/2为零,这意味着for循环中的条件会自动为假,所以循环体甚至不会执行一次。

    【讨论】:

      【解决方案3】:

      上面代码的最后一行是你得到那个异常的地方。仅当 option[x] = -a 时,您才将 aplpha 分配给 0.1。由于您在每次迭代中将 i 的值加倍,因此您的代码可能不会执行代码行 else if (option.equals("-a")) alpha = Double.valueOf(0.1);

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        相关资源
        最近更新 更多