user3086068
user3086068

Reputation: 11

Java illegal Start of an Expression error

class FishingHour
{
     public static void main(String args[])
     {
         public void fishing(){
             int totalHoursFishing = 0;
             int hoursAllowedFishing = 4;
             for(int i=1;i<25;++i)
             {
                 totalHoursFishing = ++totalHoursFishing;
                 if(hoursAllowedFishing>totalHoursFishing)
                     break;
                 System.out.println("Fishing for hours"+i+".");
             }
         }
    }
}

hey guys....i'm just a starter in java language..... my problem is, that this program is not compiling......& giving me "Illegal start of an Expression" error.......can any'one help me....??/

Upvotes: 1

Views: 1487

Answers (7)

shiva k raju
shiva k raju

Reputation: 1

It is not the correct way to declare a method inside another instead go for method calling and declared outside the main() but inside your class "FishingHour"...

Upvotes: 0

Lijo
Lijo

Reputation: 6788

public class NewClassa {
     public void fishing(){
             int totalHoursFishing = 0;
             int hoursAllowedFishing = 4;
             for(int i=1;i<25;++i)
             {

                 totalHoursFishing = ++totalHoursFishing;
                 if(hoursAllowedFishing>totalHoursFishing)
                     break;
                 System.out.println("Fishing for hours"+i+".");
             }

     }
  public static void main(String args[])
     {


         NewClassa classa=new NewClassa();
         classa.fishing();
         }


}

main is a function and you have written a new function inside main function that was the error. any way the code is note correct because the if condition is satisfied in the first loop itself and it control goes out of the loop. break means stop looping use continue then it will skip the current iteration and move to the next iteration

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13854

you can not write one method inside another another method.Java does not support nested methods.Move your fishing() outside the main method.

basic structure

class x
{
     public static void main(String args[])
     {
         //codes
     }
     public void method()
     {
        //codes
     }
}

Upvotes: 2

Aniket Thakur
Aniket Thakur

Reputation: 69025

Methods cannot be nested! main() is a special type of method from which the program starts. Separate fishing() method.

Upvotes: 0

Saša Šijak
Saša Šijak

Reputation: 9331

You can not have a method inside another one in Java. So you must put fishing() method outside main() method. For example put it above the main() method in your class.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

There is a method inside a method.

you can't do this

fishing() is inside main(). you can't have nested method.

Upvotes: 1

Kayaman
Kayaman

Reputation: 73578

You have your method fishing() inside the main() method. Methods don't nest that way.

Upvotes: 3

Related Questions