943,833 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3411
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 3rd, 2008
0

illegal start of expression

Expand Post »
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);
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jan 4th, 2008
0

Re: illegal start of expression

Hi freddiecool and welcome to DaniWeb,

Not sure if this is your problem, but your variables should be initialised as private. ie:
java Syntax (Toggle Plain Text)
  1. private pryDialog pd = new pryDialog();
  2. private int option = 0;
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Jan 4th, 2008
0

Re: illegal start of expression

At which line do you get the Error?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jan 4th, 2008
0

Re: illegal start of expression

Click to Expand / Collapse  Quote originally posted by darkagn ...
Hi freddiecool and welcome to DaniWeb,

Not sure if this is your problem, but your variables should be initialised as private. ie:
java Syntax (Toggle Plain Text)
  1. private pryDialog pd = new pryDialog();
  2. private int option = 0;
No, those are local variables inside the method. They do not have access modifiers like "private".
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Jan 4th, 2008
0

Re: illegal start of expression

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 4th, 2008
0

Re: illegal start of expression

you might want to check if the error is in pryDialog() because to me, your code should not give you an error
Reputation Points: 8
Solved Threads: 2
Newbie Poster
uonsin is offline Offline
17 posts
since Oct 2007
Jan 4th, 2008
0

Re: illegal start of expression

Click to Expand / Collapse  Quote originally posted by uonsin ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 4th, 2008
0

Re: illegal start of expression

Click to Expand / Collapse  Quote originally posted by bugmenot ...
No, those are local variables inside the method. They do not have access modifiers like "private".
Oops ... good point.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Jan 4th, 2008
0

Re: illegal start of expression

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jan 4th, 2008
0

Re: illegal start of expression

your
Java Syntax (Toggle Plain Text)
  1. void skapaPrylar(){
method is not closed. add another
Java Syntax (Toggle Plain Text)
  1. }
above the
Java Syntax (Toggle Plain Text)
  1. public static void main ....
line
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java compilerhttp://www.daniweb.com/forums/dani-images/icons/help.gif
Next Thread in Java Forum Timeline: Tutorials





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC