Katja Siamionava
Katja Siamionava

Reputation: 23

Why is Java skipping the For statement?

I'm new to Java. I'm working with the code that implements aspect-sentiment analysis of the text. I know that the code should work but somehow I keep getting the following error.

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

Here is the code of my main function up til the line where I get the error

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

When I execute the program none of if or else if lines seem to run. What can be the source of the problem? Thank you!

Upvotes: 2

Views: 238

Answers (3)

J-Alex
J-Alex

Reputation: 7107

[] args can never be null, but if you don't passing any arguments when running your application it will be empty array.

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

When your condition is false before first iteration (args.lenght is 0 and i is 0. 0 is not less than 0) - the application will not enter the loop.

enter image description here

Upvotes: 2

Nagesh Lakinepally
Nagesh Lakinepally

Reputation: 66

The last line in the above code is where you getting that exception. You are assigning aplpha to 0.1, only when option[x] = -a. Since you are doubling value of i in each iteration, your code may not be executing code line else if (option.equals("-a")) alpha = Double.valueOf(0.1);

Upvotes: 0

Jim Kiley
Jim Kiley

Reputation: 3652

If there are no args, then args.length/2 is zero, which means that the conditional in the for loop will automatically be false, so the loop body won't execute even once.

Upvotes: 0

Related Questions