Results 1 to 9 of 9
  1. #1
    chhoton is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default 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();
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What are the two statements
    Java Code:
    inFile.close();
    outFile.close();
    supposed to be doing?
    Close the PrintWriter in the last method that it is used.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Post the full error and not your summary of it?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 04:57 PM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What? You still have those lines there?
    Did you read my first reply at all then?

  7. #7
    chhoton is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    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.
    Attached Thumbnails Attached Thumbnails Error:identifier expected(Help!)-error_image.jpg  

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    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:

    A.java:5: cannot find symbol
    symbol : class FileNotFoundException
    location: class A
    public static void main(String args[]) throws FileNotFoundException {
    Which tells you the error ("cannot find symbol"), what it can't find ("symbol : class FileNotFoundException"), and where it occurred.
    Last edited by Tolls; 09-22-2009 at 05:30 PM.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by chhoton View Post
    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.
    If you only you could read my reply#5 above.

Similar Threads

  1. identifier expected
    By tlouvierre in forum New To Java
    Replies: 4
    Last Post: 05-28-2009, 01:11 AM
  2. Replies: 3
    Last Post: 12-05-2008, 09:34 PM
  3. getting identifier expected error . help me !
    By victorkeath in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 06:49 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 06:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •