2017-06-02 42 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]; ***如果没有给定参数,将会崩溃 –

+3

@ΦXocę웃Пepeúpaо否,如果未给定参数,则不会进入循环。 – Eran

回答

2

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

当使用这个版本的for语句,请记住:

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

当你的条件是假的第一次迭代(args.lenght是0和i 0 0不小于0)前 - 应用程序不会进入循环。

enter image description here

0

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

0

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