944,116 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14812
  • Java RSS
Mar 27th, 2006
0

Why illegal start of expression error?

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  1. public static String getName()

--and--

Java Syntax (Toggle Plain Text)
  1. public static void displayResults()



Here is my first class (that includes this code):

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class ProductSurvey
  5. {
  6. public static void main(String [] args)
  7. {
  8.  
  9. ProductData myData = new ProdcutData();
  10.  
  11. String name = getName();
  12. openFile();
  13. myData.setName(name);
  14. myData.dataRetrieve(name);
  15. myData.updateAverages(rating1totalLow, rating2totalLow, rating3totalLow, rating1totalMed, rating2totalMed, rating3totalMed, rating1totalHigh, rating2totalHigh, rating3totalHigh, inc1total, inc2total, inc3total);
  16. mydata.setRating2ave1lower3(rating2lower1than3, lower1than3Total);
  17. displayResults();
  18.  
  19.  
  20. public static String getName()
  21. {
  22. System.out.println("Please enter income and product info file name: ");
  23. Scanner keyboard = new Scanner(System.in);
  24. String name = keyboard.next();
  25. return name;
  26. }
  27.  
  28. public static void openFile();
  29. {
  30.  
  31. File fileObject = new File(name);
  32. while ((! fileObject.exists()) || ( ! fileObject.canRead()))
  33.  
  34. {
  35. if( ! fileObject.exists())
  36. {
  37. System.out.println("No such file");
  38. }
  39. else
  40. {
  41. System.out.println("That file is not readable.");
  42. }
  43.  
  44. System.out.println("Enter file name again:");
  45. name = keyboard.next();
  46. fileObject = new File(name);
  47. }
  48.  
  49. }
  50.  
  51.  
  52. public static void displayResults()
  53. {
  54.  
  55. 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");
  56. System.out.println("*Average (rounded) product ratings, by income bracket, are as follows: ");
  57. System.out.println(myData.toString());
  58. System.out.print("\n*Total number of persons in Income Bracket $50000-$74999 ");
  59. System.out.print("that rated all three products with a score of 5 ");
  60. System.out.println("or higher: " + myData.getHighRaters());
  61. System.out.print("\n*Average (rounded) rating for Product 2 by ");
  62. System.out.println("persons who rated Product 1 lower than Product 3: " + myData.rating2ave1lower3);
  63.  
  64. }
  65.  
  66. }
  67.  
  68. }

Here is the backup class (just for your reference):

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class ProductData
  5. {
  6. private String name;
  7. private int lineCount;
  8. private double inc1total;
  9. private double inc2total;
  10. private double inc3total;
  11. private int rating1totalLow;
  12. private int rating2totalLow;
  13. private int rating3totalLow;
  14. private int rating1totalMed;
  15. private int rating2totalMed;
  16. private int rating3totalMed;
  17. private int rating1totalHigh;
  18. private int rating2totalHigh;
  19. private int rating3totalHigh;
  20. private int highRaters;
  21. private double lower1than3Total;
  22. private int rating2lower1than3;
  23. private long rating2ave1lower3;
  24. private long rating1averageLow;
  25. private long rating2averageLow;
  26. private long rating3averageLow;
  27. private long rating1averageMed;
  28. private long rating2averageMed;
  29. private long rating3averageMed;
  30. private long rating1averageHigh;
  31. private long rating2averageHigh;
  32. private long rating3averageHigh;
  33.  
  34. public ProductData()
  35. {
  36. name = null;
  37. lineCount = 0;
  38. inc1total = 0;
  39. inc2total = 0;
  40. inc3total = 0;
  41. rating1totalLow = 0;
  42. rating2totalLow = 0;
  43. rating3totalLow = 0;
  44. rating1totalMed = 0;
  45. rating2totalMed = 0;
  46. rating3totalMed = 0;
  47. rating1totalHigh = 0;
  48. rating2totalHigh = 0;
  49. rating3totalHigh = 0;
  50. highRaters = 0;
  51. lower1than3Total= 0;
  52. rating2lower1than3 = 0;
  53. rating1averageLow = 0;
  54. rating2averageLow = 0;
  55. rating3averageLow = 0;
  56. rating1averageMed = 0;
  57. rating2averageMed = 0;
  58. rating3averageMed = 0;
  59. rating1averageHigh = 0;
  60. rating2averageHigh = 0;
  61. rating3averageHigh = 0;
  62. }
  63.  
  64. public void setName(String newName)
  65. {
  66. String name = newName;
  67. }
  68.  
  69. public void dataRetrieve(String name)
  70. {
  71. try
  72. {
  73. BufferedReader inputStream = new BufferedReader(new FileReader(name));
  74.  
  75. String trash = "No trash yet";
  76. while ((trash = inputStream.readLine()) !=null)
  77. {
  78. StringTokenizer st = new StringTokenizer(trash);
  79. int income = Integer.parseInt(st.nextToken());
  80. int rating1 = Integer.parseInt(st.nextToken());
  81. int rating2 = Integer.parseInt(st.nextToken());
  82. int rating3 = Integer.parseInt(st.nextToken());
  83.  
  84. if(rating1<rating3)
  85. {
  86. lower1than3Total++;
  87. rating2lower1than3 = rating2lower1than3 + rating2;
  88. }
  89.  
  90. if(income<50000)
  91. {
  92. rating1totalLow = rating1totalLow + rating1;
  93. rating2totalLow = rating2totalLow + rating2;
  94. rating3totalLow = rating3totalLow + rating3;
  95. inc1total++;
  96.  
  97. }
  98.  
  99. else if(income<75000)
  100. {
  101. rating1totalMed = rating1totalMed + rating1;
  102. rating2totalMed = rating2totalMed + rating2;
  103. rating3totalMed = rating3totalMed + rating3;
  104. inc2total++;
  105. if((rating1>=5) && (rating2>=5) && (rating3>=5))
  106. {
  107. highRaters++;
  108. }
  109.  
  110. }
  111.  
  112. else if(income<100000)
  113. {
  114. rating1totalHigh = rating1totalHigh + rating1;
  115. rating2totalHigh = rating2totalHigh + rating2;
  116. rating3totalHigh = rating3totalHigh + rating3;
  117. inc3total++;
  118. }
  119.  
  120. lineCount++;
  121.  
  122. }
  123.  
  124. inputStream.close();
  125. }
  126. catch(IOException e)
  127. {
  128. System.out.println("Problem reading from file.");
  129. }
  130. }
  131.  
  132. 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)
  133. {
  134. rating1averageLow = Math.round(rating1totalLow/inc1total);
  135. rating2averageLow = Math.round(rating2totalLow/inc1total);
  136. rating3averageLow = Math.round(rating3totalLow/inc2total);
  137. rating1averageMed = Math.round(rating1totalMed/inc2total);
  138. rating2averageMed = Math.round(rating2totalMed/inc2total);
  139. rating3averageMed = Math.round(rating3totalMed/inc2total);
  140. rating1averageHigh = Math.round(rating1totalHigh/inc3total);
  141. rating2averageHigh = Math.round(rating2totalHigh/inc3total);
  142. rating3averageHigh = Math.round(rating3totalHigh/inc3total);
  143. }
  144.  
  145. public long setRating2ave1lower3(int rating2lower1than3, double lower1than3Total)
  146. {
  147. long rating2ave1lower3 = (rating2lower1than3/lower1than3Total);
  148. return rating2ave1lower3;
  149. }
  150.  
  151. public String toString()
  152. {
  153. return ("\nIncome level $26000-$49999:" + "\n" + "-Product 1: "
  154. + rating1AverageLow + "-Product 2: " + rating2AverageLow
  155. + "-Product 3: " + rating3AverageLow + "\n" + "\n" + "Income level $50000-$74999:" + "\n" + "-Product 1: "
  156. + rating1AverageMed + "-Product 2: " + rating2AverageMed
  157. + "-Product 3: " + rating3AverageMed + "\n" + "\n" + "Income level $75000-$100000:" + "\n" + "-Product 1: "
  158. + rating1AverageHigh + "-Product 2: " + rating2AverageHigh
  159. + "-Product 3: " + rating3AverageHigh);
  160. }
  161.  
  162. public int getHighRaters()
  163. {
  164. return highRaters;
  165. }
  166.  
  167.  
  168. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cazaletm is offline Offline
3 posts
since Mar 2006
Mar 27th, 2006
0

Re: Why illegal start of expression error?

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

Java Syntax (Toggle Plain Text)
  1. public class TestClass
  2. {
  3. public static void main(String[] args)
  4. {
  5. }
  6.  
  7. public static String getName()
  8. {
  9. }
  10.  
  11. public static void openFile()
  12. {
  13. }
  14.  
  15. public static void displayResults()
  16. {
  17. }
  18. }
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Jul 3rd, 2011
0

solution to illegal expression in java

hello buddy, you are trying to write method within main method, put your method ou of your main method in same class and try out, i hope it'll help..
me too had same prblm solved with this solution
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dhanesh10 is offline Offline
20 posts
since Oct 2008
Jul 3rd, 2011
0
Re: Why illegal start of expression error?
It looks like this post has been solved , why is still showing unsolved ???
Reputation Points: 4
Solved Threads: 6
Newbie Poster
javinpaul is offline Offline
20 posts
since Oct 2010
Jul 4th, 2011
0
Re: Why illegal start of expression error?
because the OP hasn't marked it as "solved" yet.
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Jul 4th, 2011
0
Re: Why illegal start of expression error?
yes, u need to mark it as solved if the if the u are satisfied with the solution given by any of the users
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dhanesh10 is offline Offline
20 posts
since Oct 2008
Jul 5th, 2011
0
Re: Why illegal start of expression error?
or you should have watched at the date, and realised this thread is about 5 years old.
there wasn't really a point in reviving it, since the answer to the OP's problem was given in 2006. if he/she didn't mark it as solved back then chances are pretty big it won't happen now.

if an admin reads this, I think it's pretty save to close this thread.
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Jul 6th, 2011
0
Re: Why illegal start of expression error?
in fact OP most likely doesn't even remember ever visiting this site, if it even remembers the site exists.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Help! java.lang.UnsatisfiedLinkError
Next Thread in Java Forum Timeline: Mastering Java





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


Follow us on Twitter


© 2011 DaniWeb® LLC