| | |
illegal start of expression
Thread Solved |
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);
}
}
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);
}
}
Hi freddiecool and welcome to DaniWeb,
Not sure if this is your problem, but your variables should be initialised as private. ie:
Not sure if this is your problem, but your variables should be initialised as private. ie:
java Syntax (Toggle Plain Text)
private pryDialog pd = new pryDialog(); private int option = 0;
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 32
•
•
•
•
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)
private pryDialog pd = new pryDialog(); private int option = 0;
--
Index of mp3
Index of mp3
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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.
(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.
your
method is not closed. add another
above the
line
Java Syntax (Toggle Plain Text)
void skapaPrylar(){
Java Syntax (Toggle Plain Text)
}
Java Syntax (Toggle Plain Text)
public static void main ....
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- illegal start of expression (Java)
- JAVA Illegal Start of Expression Error (Java)
- Illegal Start of Expression (Java)
- illegal start of expression (Java)
- Fibonacci Recursion - Illegal Start of expression! (Java)
- illegal start of expression (Java)
- Why illegal start of expression error? (Java)
Other Threads in the Java Forum
- Previous Thread: java compilerhttp://www.daniweb.com/forums/dani-images/icons/help.gif
- Next Thread: Tutorials
Views: 2871 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Java
add android applet application arguments array arraylist arrays binary c# c++ chat class classes client code component constructor convert data database db design desktop detection eclipse error event exception file forloop fractal givemetehcodez graphics gridlayout gui helpwithhomework homeworkassignment html ide image inheritance input integer interface j2me java javafx jframe jpanel jtextarea jtextfield key lazy linked linked-list list loop looping method methods mobile netbeans newbie node number object oracle output page parameter pattern pixel problem programming read recursion remove return robot scanner search server service set size sms sql string swing system text text-file threads timer transfer translate tree user web






