-4

I must be missing something here but i cannot see where the error is in this piece of code keeps saying Test.java:63: error: illegal start of expression

                            }else 
                            ^

Test.java:63: error: 'else' without 'if'

                           }else



public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge){
                for(int index = 0; index < count; index ++){   
                      if (gpa[index] == 4.00){
                            awardsum += 1000;
                                if(need[index] == true){
                            awardsum += 500;
                                }else awardsum += 200;    
                      }else 
                      if (gpa[index] <= 3.70 && gpa[index] < 4.00){
                            if (need[index] == true){
                                   awardsum += 500;
                                if (gpa[index] >= classaverage){
                                   awardsum += 500;
                                }else     
                           }else
                      }else                              
                      if (gpa[index] >= classaverage){
                         awardsum += 200;
                      }else

                      if (gpa[index] >= classaverage){
                            if (need[index] == true){
                                awardsum += 500;
                            }else 
                                awardsum += 200;
                      }else 
                 award[index] = awardsum;
                 return award;                                         
                }
        }
Makominami
  • 21
  • 1
  • 1
  • 5
  • 5
    Often the problem is poor code indentation preventing you from seeing if ifs and elses are matched. Fix this with care. But yeah, your code doesn't make sense as you've got a dangling `else` at the top. – Hovercraft Full Of Eels Sep 09 '13 at 01:07
  • Your `else`s don't make sense. – SLaks Sep 09 '13 at 01:07
  • 2
    In fact that first else appears to be dangling out in space, outside of any and all methods and constructors. Please strive to post your actual code with your questions unless your goal is just to confuse and confound us. – Hovercraft Full Of Eels Sep 09 '13 at 01:10
  • You miss placed the return statement. It returns the award in first iteration. Place it after the for loop – newuser Sep 09 '13 at 01:25

3 Answers3

2

Open bracket after else?

if(){

} else {

)
Justin Chang
  • 194
  • 12
1

I think there is a problem with your understanding of if/else construct. You may or may not need an else scenario, so no need to use else in that case. As you have put multiple such else in your code, which seems to be of no use. It seems you just use it as if it is mandated to use else with if. Also always use curly braces to start and end if or else block to avoid errors.

Following marked else seems unnecessary:

     if (gpa[index] >= classaverage){
         awardsum += 500;
      }else     // seems not required
 }else // seems not required
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

How to use if-else statement

   if (gpa[index] >= classaverage)
        {
             awardsum += 500;
        }
         else //unwanted else    
     }else //unwanted else
    }else                              
    if (gpa[index] >= classaverage){

You also made the mistake while returning the return award;. You placed with in the loop. Place it after the loop ends

This the formatted code try it

public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count, double classadvarge)
    {
        for (int index = 0; index < count; index++)
        {
            if (gpa[index] == 4.00)
            {
                awardsum += 1000;
                if (need[index] == true)
                {
                    awardsum += 500;
                }
                else
                {
                    awardsum += 200;
                }
            }
            else if (gpa[index] <= 3.70 & gpa[index] < 4.00)
            {
                if (need[index] == true)
                {
                    awardsum += 500;
                    if (gpa[index] >= classaverage)
                    {
                        awardsum += 500;
                    }
                    else //unused else
                    {

                    }
                }
                else //unused else
                {

                }
            }
            else if (gpa[index] >= classaverage)
            {
                awardsum += 200;
            }
            else if (gpa[index] >= classaverage)
            {
                if (need[index] == true)
                {
                    awardsum += 500;
                }
                else
                {
                    awardsum += 200;
                }
            }
            else
            {


        award[index] = awardsum;
                }
//            return award; // you wrongly placed the return statement
            }
            return award;  // This will return the whole array
        }
newuser
  • 8,338
  • 2
  • 25
  • 33