Hi can anyone help me i get an illegal start of expression public static void main everytime i try to compile.My code is below.
Thanks...

public static void main (String []args){
PrylDialog pd=new PrylDialog();
System.out.println("Welcome!!!!! ");
int option=0;
do{
option=pd.kommando();
switch(option){
case 1:pd.skapaPerson();
break;
case 2: pd.skapaPryl();
break;
case 3: pd.visaAlla();
break;
case 4: pd.visaRikast();
break;
case 5: pd.visaVissPerson();
break;
case 6: pd.börskrasch();
break;
case 7: System.out.println("That's all folks... ");
}
}
while (option!=7);
}
}

Dani AI

Generated

The compiler error at the main line is a symptom, not the root cause. As pointed out, Java reports "illegal start of expression" for a method declaration when the parser is still inside an earlier method or statement. The first step is to fix unmatched braces: use your editor's brace-matching or auto‑format to locate the missing } that closes the previous method (skapaPrylar). Once that is closed, the main declaration will be parsed correctly.

The follow‑up "cannot find symbol" errors are separate, and come from simple typos and missing declarations (several people touched on these). Things to fix next:

  • Declare the collection you iterate over (prylar) at class level and use the correct class name casing:
    private ArrayList<Prylar> prylar = new ArrayList<Prylar>();
  • Make sure called method names match the definitions exactly (Java is case‑sensitive). If you defined skapaPrylar() but call skapaPryl(), the compiler will complain.
  • Implement any abstract methods declared in the base class (Prylar). For example, if getVarde() is abstract you must provide an override in Smycke (and other concrete subclasses):
    @Override
    public int getVarde() {
      // compute and return this object's value
      return /* value */;
    }

Extra tips from the thread: local variables cannot have access modifiers (see ), ArrayList is case‑sensitive (), and watch logic errors like using && when you meant || for out‑of‑range checks:

if (slit < 1 || slit > 10) { /* handle error */ }

Fix braces first, recompile, then address the symbol/typo/abstract‑method errors one at a time. This iterative approach will make the remaining issues visible and easy to resolve.

Recommended Answers

All 17 Replies

Hi freddiecool and welcome to DaniWeb,

Not sure if this is your problem, but your variables should be initialised as private. ie:

private pryDialog pd = new pryDialog();
private int option = 0;

At which line do you get the Error?

Hi freddiecool and welcome to DaniWeb,

Not sure if this is your problem, but your variables should be initialised as private. ie:

private pryDialog pd = new pryDialog();
private int option = 0;

No, those are local variables inside the method. They do not have access modifiers like "private".

We need to see what comes directly before the method. That error normally occurrs because you've done something wrong before that, such as not closing a previous method, or forgetting a semicolon on the previous line, etc.

you might want to check if the error is in pryDialog() because to me, your code should not give you an error

you might want to check if the error is in pryDialog() because to me, your code should not give you an error

That error is a compiler error, and as I have said, it is normally triggered by what came before. I.e. the previous method does not have a closing brace (so you then are effectively trying to declare a method within a method) or the last statement is missing the semicolon (which then means that the method declaration is effectively part of the previous statement) are the two main causes of this.

No, those are local variables inside the method. They do not have access modifiers like "private".

Oops ... good point. :$

This is my whole code, if anyone could spot the problem.Im not sure which line it is but it is an illegal start of expresion public static void main(String []args){ at the end of the code.

import java.util.*;
import java.io.*;
import java.lang.*;

class PrylDialog {
   ArrayList<Person> personer=new ArrayList<Person>();

   Scanner scan=new Scanner(System.in);

   String readString(String fraga){
      System.out.print(fraga);
      String str=scan.nextLine();
      return str;
     }

   int readInt (String fraga){
      for(;;){

     try{
        System.out.print(fraga);
        int x= Integer.parseInt(scan.nextLine());
        return x;
     }
     catch (NumberFormatException e){
        System.out.println("Fel - skall vara numerisk värde ");
     }
      }
   }

   Person getPerson(String na){
      for (Person p : personer)
     if (p.getNamn().equalsIgnoreCase(na))
        return p;
      return null; 
   }
   Prylar getPrylar(String prylr){
      for (Prylar pro : prylar)
      if (pro.getPrylNamn().equalsIgnoreCase(prylr))
      return pro;
      return null;
      }

    int kommando(){
      for(;;){
     int option=readInt("\n1 Skapa person\n2 Skapa pryl\n3 Visa alla\n4 Visa rikast\n5 Visa viss person\n6 Börskrasch\n7 Avsluta\n\n Ditt val: ");
     if (option>=1 && option<=7)
        return option;
     else 
     System.out.println("Felaktig kommando, ska vara 1-7\\n"); 
      }
   }

   void skapaPerson(){
      String namn=readString("Namn: ");;
      if (getPerson(namn)!=null){
     System.out.println ("Person med namn-"+namn+" finns redan");
     return;
      }
      Person pers= new Person(namn);
      personer.add(pers);
   }

   void skapaPrylar(){
      String pryl = readString("Vilken sorts pryl: ");
      String namn = readString("Vilken person äger prylen: ");
      Person p=getPerson(namn);
      if (p==null){
     System.out.println("Det finns inget person som heter "+namn);
     return;
      }

      if (pryl.equalsIgnoreCase("Smycke")){
     int v=readInt("Vikt: ");
        int sten=readInt("Antal ädelstenar: ");
        Smycke sm=new Smycke(pryl, v, sten);
        getPerson(namn).prylar.add(sm);
  }
         else if (pryl.equalsIgnoreCase("apparater")){
     int inkop =readInt("Pris: ");
     int slit=readInt("Värde (1-10): ");
     if (slit<1 && slit>10)
        System.out.println("You Wroonnng FOOL!!!");
     return;
     Apparater ap= new Apparater(pryl, inkop, slit);
     getPerson(namn).prylar.add(ap);
       }
   else if  (pryl.equalsIgnoreCase("Aktie")){
     int pris=readInt("Pris: ");
     int antAkt=readInt("Antal aktier: ");
     Aktie akt=new Aktie(pryl, antAkt, pris);
     getPerson(namn).prylar.add(akt);
      }  

    [U]public static void main (String []args){ [/U]
        ^

      PrylDialog pd=new PrylDialog();
      System.out.println("Welcome!!!!! ");
      int option=0;
      do{
         option=pd.kommando();
     switch(option){
     case 1:pd.skapaPerson();
        break;   
     case 2: pd.skapaPryl();
        break;
     case 3: pd.visaAlla();
        break;
     case 4: pd.visaRikast();
        break;
     case 5: pd.visaVissPerson(); 
        break;
     case 6: pd.börskrasch();
        break;
     case 7: System.out.println("That's all folks... ");
     }
      }

     while(option !=7);

    };   
}
}

Ps. if you see a smiley in the code, it is a ';'.Not sure how to get rid of it.
thanks for the help.

your

void skapaPrylar(){

method is not closed. add another

}

above the

public static void main ....

line

This is my whole code, if anyone could spot the problem.Im not sure which line it is but it is an illegal start of expresion "public static void main(String []args){" at the end of the code.
(It is underlined). ^

import java.util.*;
import java.io.*;
import java.lang.*;

class PrylDialog {
   ArrayList<Person> personer=new ArrayList<Person>();

   Scanner scan=new Scanner(System.in);

   String readString(String fraga){
      System.out.print(fraga);
      String str=scan.nextLine();
      return str;
	 }

   int readInt (String fraga){
      for(;;){

	 try{
	    System.out.print(fraga);
	    int x= Integer.parseInt(scan.nextLine());
	    return x;
	 }
	 catch (NumberFormatException e){
	    System.out.println("Fel - skall vara numerisk värde ");
	 }
      }
   }

   Person getPerson(String na){
      for (Person p : personer)
	 if (p.getNamn().equalsIgnoreCase(na))
	    return p;
      return null; 
   }
   Prylar getPrylar(String prylr){
      for (Prylar pro : prylar)
      if (pro.getPrylNamn().equalsIgnoreCase(prylr))
      return pro;
      return null;
      }

    int kommando(){
      for(;;){
	 int option=readInt("\n1 Skapa person\n2 Skapa pryl\n3 Visa alla\n4 Visa rikast\n5 Visa viss person\n6 Börskrasch\n7 Avsluta\n\n Ditt val: ");
	 if (option>=1 && option<=7)
	    return option;
	 else 
	 System.out.println("Felaktig kommando, ska vara 1-7\\n"); 
      }
   }

   void skapaPerson(){
      String namn=readString("Namn: ");;
      if (getPerson(namn)!=null){
	 System.out.println ("Person med namn-"+namn+" finns redan");
	 return;
      }
      Person pers= new Person(namn);
      personer.add(pers);
   }

   void skapaPrylar(){
      String pryl = readString("Vilken sorts pryl: ");
      String namn = readString("Vilken person äger prylen: ");
      Person p=getPerson(namn);
      if (p==null){
	 System.out.println("Det finns inget person som heter "+namn);
	 return;
      }
  
      if (pryl.equalsIgnoreCase("Smycke")){
	 int v=readInt("Vikt: ");
	    int sten=readInt("Antal ädelstenar: ");
	    Smycke sm=new Smycke(pryl, v, sten);
	    getPerson(namn).prylar.add(sm);
  }
         else if (pryl.equalsIgnoreCase("apparater")){
	 int inkop =readInt("Pris: ");
	 int slit=readInt("Värde (1-10): ");
	 if (slit<1 && slit>10)
	    System.out.println("You Wroonnng FOOL!!!");
	 return;
	 Apparater ap= new Apparater(pryl, inkop, slit);
	 getPerson(namn).prylar.add(ap);
       }
   else if  (pryl.equalsIgnoreCase("Aktie")){
	 int pris=readInt("Pris: ");
	 int antAkt=readInt("Antal aktier: ");
	 Aktie akt=new Aktie(pryl, antAkt, pris);
	 getPerson(namn).prylar.add(akt);
      }  

    [U]public static void main (String []args){ [/U]
        ^
  
      PrylDialog pd=new PrylDialog();
      System.out.println("Welcome!!!!! ");
      int option=0;
      do{
       	 option=pd.kommando();
	 switch(option){
	 case 1:pd.skapaPerson();
	    break;   
	 case 2: pd.skapaPryl();
	    break;
	 case 3: pd.visaAlla();
	    break;
	 case 4: pd.visaRikast();
	    break;
	 case 5: pd.visaVissPerson(); 
	    break;
	 case 6: pd.börskrasch();
	    break;
	 case 7: System.out.println("That's all folks... ");
	 }
      }
      
	 while(option !=7);
	 
    };   
}
}

Ps. if you see a smiley in the code, it is a ';'.Not sure how to get rid of it.
thanks for the help.

By using code tags.

Thanks that helped, i must have removed by mistake when i tried to fix this problem.Why cant the compiler find "prylar" but it can find "Person p: personer"?

PrylDialog.java:38: cannot find symbol
symbol : variable prylar
location: class PrylDialog
for (Prylar pro : prylar)
^
PrylDialog.java:104: cannot find symbol
symbol : method skapaPryl()
location: class PrylDialog
case 2: pd.skapaPryl();
^
PrylDialog.java:106: cannot find symbol
symbol : method visaAlla()
location: class PrylDialog
case 3: pd.visaAlla();

Prylar getPrylar(String prylr){
      for (Prylar pro : prylar)
void skapaPrylar(){

symbol : method skapaPryl()

Notice the spelling.

I still cant find prylar. Do i have to declare prylar somewhere?Maybe innitiate an ArrayList like i did for (PERSON : personer)?


PrylDialog.java:38: cannot find symbol
symbol : variable prylar
location: class PrylDialog
for (Prylar pro : prylar)
^

Yes, I don't see that you have declared "prylar" anywhere, yet you are using it as a class-level variable.

Im very new, im not quite sure where to declare it.I have another class by the name" class Prylar".I tried to make an ArrayList like i did for (PERSON:personer) but i gor thge following error.

PrylDialog.java:8: cannot find symbol
symbol  : class Arraylist
location: class PrylDialog
   Arraylist<Prylar> prylar= new Arraylist<Prylar>();
   ^
PrylDialog.java:8: cannot find symbol
symbol  : class Arraylist
location: class PrylDialog
   Arraylist<Prylar> prylar= new Arraylist<Prylar>();
                                 ^
c:\oop\html\up2\Smycke.java:1: Smycke is not abstract and does not override abstract method getVarde() in Prylar
 class Smycke extends Prylar{
 ^
3 errors

Check your spelling. The correct class is "ArrayList".

The last error means that you've extended a class that has an abstract method in it, which requires you to implement that method, but you have not written that method in your class.

Cheers for the help...;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.