954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

illegal start of expression

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

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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;
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

At which line do you get the Error?

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

uonsin
Newbie Poster
17 posts since Oct 2007
Reputation Points: 8
Solved Threads: 2
 
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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
No, those are local variables inside the method. They do not have access modifiers like "private".

Oops ... good point. :$

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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 personer=new ArrayList();

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

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

};
}
}


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

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

your

void skapaPrylar(){

method is not closed. add another

}

above the

public static void main ....

line

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

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

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
Prylar getPrylar(String prylr){
      for (Prylar pro : prylar)
void skapaPrylar(){

symbol : method skapaPryl()


Notice the spelling.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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= new Arraylist();
^
PrylDialog.java:8: cannot find symbol
symbol : class Arraylist
location: class PrylDialog
Arraylist prylar= new Arraylist();
^
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

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Cheers for the help...;)

freddiecool
Light Poster
37 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You