#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Neted IFs error with ELSE


    I am getting an error when writing this code. The error is "Else without if" in NetBeans.

    Code:
    public double SomeClass {
    
    if (var1 >= (var2 * var3))
    {
        if var1 > var2
       {
        // some math code here
       }
       else
       {
       // some code here
      }
    // ERROR. The following else says else without if in Netbeans
    else
    {
      // some code here
    }
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    the underlying problem is that your formatting is a mess. Consequently, you messed up the braces as well.

    Fix your formatting, and the error should become obvious. I guess you wanted something like this:

    java Code:
    if (var1 >= var2 * var3)
    {
    	if var1 > var2
    	{
    		// some math code here
    	}
    	else
    	{
    		// some code here
    	}
    }
    else
    {
    	// some code here
    }

    Which again proves that formatting isn't just beautifying. It's crucial for the correctness and readability of code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    Thank you very much. Lesson learned.

IMN logo majestic logo threadwatch logo seochat tools logo