| | |
Why illegal start of expression error?
![]() |
•
•
Join Date: Mar 2006
Posts: 3
Reputation:
Solved Threads: 0
Hello great java minds. Could you please tell me why I get "illegal start of expression" errors for the following headers? Thanks for your wisdom!!
Lines generating this error:
--and--
Here is my first class (that includes this code):
Here is the backup class (just for your reference):
Lines generating this error:
Java Syntax (Toggle Plain Text)
public static String getName()
--and--
Java Syntax (Toggle Plain Text)
public static void displayResults()
Here is my first class (that includes this code):
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class ProductSurvey { public static void main(String [] args) { ProductData myData = new ProdcutData(); String name = getName(); openFile(); myData.setName(name); myData.dataRetrieve(name); myData.updateAverages(rating1totalLow, rating2totalLow, rating3totalLow, rating1totalMed, rating2totalMed, rating3totalMed, rating1totalHigh, rating2totalHigh, rating3totalHigh, inc1total, inc2total, inc3total); mydata.setRating2ave1lower3(rating2lower1than3, lower1than3Total); displayResults(); public static String getName() { System.out.println("Please enter income and product info file name: "); Scanner keyboard = new Scanner(System.in); String name = keyboard.next(); return name; } public static void openFile(); { File fileObject = new File(name); while ((! fileObject.exists()) || ( ! fileObject.canRead())) { if( ! fileObject.exists()) { System.out.println("No such file"); } else { System.out.println("That file is not readable."); } System.out.println("Enter file name again:"); name = keyboard.next(); fileObject = new File(name); } } public static void displayResults() { System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); System.out.println("*Average (rounded) product ratings, by income bracket, are as follows: "); System.out.println(myData.toString()); System.out.print("\n*Total number of persons in Income Bracket $50000-$74999 "); System.out.print("that rated all three products with a score of 5 "); System.out.println("or higher: " + myData.getHighRaters()); System.out.print("\n*Average (rounded) rating for Product 2 by "); System.out.println("persons who rated Product 1 lower than Product 3: " + myData.rating2ave1lower3); } } }
Here is the backup class (just for your reference):
Java Syntax (Toggle Plain Text)
import java.util.*; import java.io.*; class ProductData { private String name; private int lineCount; private double inc1total; private double inc2total; private double inc3total; private int rating1totalLow; private int rating2totalLow; private int rating3totalLow; private int rating1totalMed; private int rating2totalMed; private int rating3totalMed; private int rating1totalHigh; private int rating2totalHigh; private int rating3totalHigh; private int highRaters; private double lower1than3Total; private int rating2lower1than3; private long rating2ave1lower3; private long rating1averageLow; private long rating2averageLow; private long rating3averageLow; private long rating1averageMed; private long rating2averageMed; private long rating3averageMed; private long rating1averageHigh; private long rating2averageHigh; private long rating3averageHigh; public ProductData() { name = null; lineCount = 0; inc1total = 0; inc2total = 0; inc3total = 0; rating1totalLow = 0; rating2totalLow = 0; rating3totalLow = 0; rating1totalMed = 0; rating2totalMed = 0; rating3totalMed = 0; rating1totalHigh = 0; rating2totalHigh = 0; rating3totalHigh = 0; highRaters = 0; lower1than3Total= 0; rating2lower1than3 = 0; rating1averageLow = 0; rating2averageLow = 0; rating3averageLow = 0; rating1averageMed = 0; rating2averageMed = 0; rating3averageMed = 0; rating1averageHigh = 0; rating2averageHigh = 0; rating3averageHigh = 0; } public void setName(String newName) { String name = newName; } public void dataRetrieve(String name) { try { BufferedReader inputStream = new BufferedReader(new FileReader(name)); String trash = "No trash yet"; while ((trash = inputStream.readLine()) !=null) { StringTokenizer st = new StringTokenizer(trash); int income = Integer.parseInt(st.nextToken()); int rating1 = Integer.parseInt(st.nextToken()); int rating2 = Integer.parseInt(st.nextToken()); int rating3 = Integer.parseInt(st.nextToken()); if(rating1<rating3) { lower1than3Total++; rating2lower1than3 = rating2lower1than3 + rating2; } if(income<50000) { rating1totalLow = rating1totalLow + rating1; rating2totalLow = rating2totalLow + rating2; rating3totalLow = rating3totalLow + rating3; inc1total++; } else if(income<75000) { rating1totalMed = rating1totalMed + rating1; rating2totalMed = rating2totalMed + rating2; rating3totalMed = rating3totalMed + rating3; inc2total++; if((rating1>=5) && (rating2>=5) && (rating3>=5)) { highRaters++; } } else if(income<100000) { rating1totalHigh = rating1totalHigh + rating1; rating2totalHigh = rating2totalHigh + rating2; rating3totalHigh = rating3totalHigh + rating3; inc3total++; } lineCount++; } inputStream.close(); } catch(IOException e) { System.out.println("Problem reading from file."); } } public void updateAverages(int rating1totalLow, int rating2totalLow, int rating3totalLow, int rating1totalMed, int rating2totalMed, int rating3totalMed, int rating1totalHigh, int rating2totalHigh, int rating3totalHigh, long inc1total, long inc2total, long inc3total) { rating1averageLow = Math.round(rating1totalLow/inc1total); rating2averageLow = Math.round(rating2totalLow/inc1total); rating3averageLow = Math.round(rating3totalLow/inc2total); rating1averageMed = Math.round(rating1totalMed/inc2total); rating2averageMed = Math.round(rating2totalMed/inc2total); rating3averageMed = Math.round(rating3totalMed/inc2total); rating1averageHigh = Math.round(rating1totalHigh/inc3total); rating2averageHigh = Math.round(rating2totalHigh/inc3total); rating3averageHigh = Math.round(rating3totalHigh/inc3total); } public long setRating2ave1lower3(int rating2lower1than3, double lower1than3Total) { long rating2ave1lower3 = (rating2lower1than3/lower1than3Total); return rating2ave1lower3; } public String toString() { return ("\nIncome level $26000-$49999:" + "\n" + "-Product 1: " + rating1AverageLow + "-Product 2: " + rating2AverageLow + "-Product 3: " + rating3AverageLow + "\n" + "\n" + "Income level $50000-$74999:" + "\n" + "-Product 1: " + rating1AverageMed + "-Product 2: " + rating2AverageMed + "-Product 3: " + rating3AverageMed + "\n" + "\n" + "Income level $75000-$100000:" + "\n" + "-Product 1: " + rating1AverageHigh + "-Product 2: " + rating2AverageHigh + "-Product 3: " + rating3AverageHigh); } public int getHighRaters() { return highRaters; } }
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
Its because you're defining methods inside of another method.
remove the public static String getName(), public static void openFile(); (btw, you need to remove the ; here), and public static void displayResults() from the public static void main(String[] args) method.
So your code should look more like...
remove the public static String getName(), public static void openFile(); (btw, you need to remove the ; here), and public static void displayResults() from the public static void main(String[] args) method.
So your code should look more like...
Java Syntax (Toggle Plain Text)
public class TestClass { public static void main(String[] args) { } public static String getName() { } public static void openFile() { } public static void displayResults() { } }
![]() |
Similar Threads
- Illegal start of expresion (Java)
- illegal start of expression (Java)
- JAVA Illegal Start of Expression Error (Java)
- Fibonacci Recursion - Illegal Start of expression! (Java)
- illegal start of expression (Java)
- illegal start of expression (Java)
Other Threads in the Java Forum
- Previous Thread: Where can ! get some coding about Java-Security
- Next Thread: calculator program
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





