Results 1 to 9 of 9
Thread: Error:identifier expected(Help!)
- 09-22-2009, 02:36 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
Error:identifier expected(Help!)
Hi All,
I am new to Java. I have a compiler error msg while I try to compile it in Dr Java or in Netbeans. I am wondering if any one could tell me what is the error in my code? Plesae reply. Thanks in advance.
Here is the code:
public class DriverClass
{
private int[] score = new int[10];; //array to store the test scores
int i;
double sum;
double avg;
Scanner inFile = new Scanner(new FileReader("inputFile.txt"));
PrintWriter outFile = new PrintWriter("testGrade.out");
public DriverClass() //Default constructor
{
i = 0;
sum = 0;
avg = 0;
}
public void readFile() //method to from an input file
{
for(i = 0; i < score.length; i++)
score[i] = inFile.nextInt();
}
public void computeAvg() //method to compute the average score
{
for(i = 0; i < score.length; i++)
{
sum = (sum + score[i]);
}
avg = (sum / 10.0);
outFile.printf("%nThe average score is "+avg+"%n%n%n");
}
public void printGrade()
{
for(i = 0; i < score.length; i++)
{
if(score[i] > avg * 1.10)
outFile.printf("Student No.["+i+"] Score "+score[i]+" Grade: Outstanding%n%n");
else if((score[i] <= avg * 1.10) && (score[i] >= avg * 0.90))
outFile.printf("Student No.["+i+"] Score "+score[i]+" Grade: Satisfactory%n%n");
else
outFile.printf("Student No.["+i+"] Score "+score[i]+" Grade: Unsatisfactory%n%n");
}
}
inFile.close();
outFile.close();
}
- 09-22-2009, 02:50 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What are the two statements
supposed to be doing?Java Code:inFile.close(); outFile.close();
Close the PrintWriter in the last method that it is used.
- 09-22-2009, 03:16 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Post the full error and not your summary of it?
- 09-22-2009, 03:17 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You need to learn to read the error messages.
They tell you exactly what the problem is and where the problem is in your code.
You will also learn a lot on how Java works just by reading the error messages.
For the code above, the compiler told you which symbol could not be found. You didn't post that information but that is the key to the whole problem.
Most likely you didn't import the FileNotFoundException from the java.io package.
-
1) Please use code tags when posting code. Not doing so makes your code very difficult to read, and makes it hard for others to help. See my signature to see how to do this. Also, there is no need to repost your code as you can edit your current posts so that they use code tags.
2) Look at the location of your close() method calls. Tell me, what method are they currently in?
The solution: have the inFile variable declared and initialized and then closed in the method where it is used. I'd declare it early in the method setting it = to null, initialize it within a try block, and close it within a finally block (first checking that it's not null). You may do similar actions with outFile.
I believe that r035... hinted at the same thing.Last edited by Fubarable; 09-22-2009 at 03:57 PM.
- 09-22-2009, 03:59 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What? You still have those lines there?
Did you read my first reply at all then?
- 09-22-2009, 04:21 PM #7
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
Hi r035198x,
Sorry, I moved those lines inside the last method printGrade(). but it shows error in main class as shown in attachment.
I am too dumb I think, could not rectify the error till now.
Thanks everybody for your reply.
- 09-22-2009, 04:25 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Can't you copy and paste the error rather than sticking blurry images up?
Anyway, r035198x has already told you what that error means in post #5 (last line).
ETA:
Here's an example of copy and paste with the same error:
Which tells you the error ("cannot find symbol"), what it can't find ("symbol : class FileNotFoundException"), and where it occurred.A.java:5: cannot find symbol
symbol : class FileNotFoundException
location: class A
public static void main(String args[]) throws FileNotFoundException {Last edited by Tolls; 09-22-2009 at 04:30 PM.
- 09-22-2009, 04:42 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
identifier expected
By tlouvierre in forum New To JavaReplies: 4Last Post: 05-28-2009, 12:11 AM -
error"<identifier> expected" trough the use of interface
By parme in forum New To JavaReplies: 3Last Post: 12-05-2008, 08:34 PM -
getting identifier expected error . help me !
By victorkeath in forum New To JavaReplies: 3Last Post: 11-07-2008, 05:49 PM -
Identifier expected error
By vasu18 in forum New To JavaReplies: 1Last Post: 01-01-2008, 05:49 PM -
Error: <identifier> expected
By barney in forum AWT / SwingReplies: 2Last Post: 07-31-2007, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks