CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Unhappy Java Programming help

    Hello, This is my first post. I am taking a java programming class, my first, and it goes good some days and really bad the others. So I am working on an assignment and I cant figure out the 1 error I have. Any help would be nice! Thanks. The error I am getting is:

    Ch7Ex12.java:13: illegal start of expression
    public static void main(String[] args) throws FileNotFoundException
    ^
    1 error

    My code is:

    //Michael
    //Chapter 7 Example 12 assignment
    //Ch7Ex12.java
    // This program calculates GPA for male and females.

    import java.io.*;
    import java.util.*;

    public class Ch7Ex12
    {
    {

    public static void main(String[] args) throws FileNotFoundException
    {
    IntClass female = new IntClass();
    IntClass male = new IntClass();

    DoubleClass femaleGPA = new DoubleClass();
    DoubleClass maleGPA = new DoubleClass();
    DoubleClass averageFemaleGPA = new DoubleClass();
    DoubleClass averageMaleGPA = new DoubleClass();


    initialize(female, male, femaleGPA, maleGPA, averageFemaleGPA, averageMaleGPA);
    sumGrades(female, male, femaleGPA, maleGPA);
    averageGrade(female, male, femaleGPA, maleGPA, averageFemaleGPA, averageMaleGPA);
    printResults(female, male, femaleGPA, maleGPA, averageFemaleGPA, averageMaleGPA);
    }



    public static void initialize(IntClass countFemale, IntClass countMale, DoubleClass sumFemaleGPA, DoubleClass sumMaleGPA, DoubleClass averageFemaleGPA, DoubleClass averageMaleGPA)

    {
    countFemale.setNum(0);
    countMale.setNum(0);

    sumFemaleGPA.setNum(0.0);
    sumMaleGPA.setNum(0.0);
    averageFemaleGPA.setNum(0.0);
    averageMaleGPA.setNum(0.0);
    }



    public static void sumGrades(IntClass countFemale, IntClass countMale, DoubleClass sumFemaleGPA, DoubleClass sumMaleGPA) throws FileNotFoundException
    {
    Scanner inFile = new Scanner(new FileReader("Ch7Ex12Data.txt"));
    double GPA;
    char letterCode;

    while (inFile.hasNext());
    {
    letterCode = inFile.next().charAt(0);
    GPA = inFile.nextDouble();

    switch (letterCode)
    {
    case 'f': countFemale.setNum(countFemale.getNum() + 1);
    sumFemaleGPA.setNum(sumFemaleGPA.getNum() + GPA);
    break;

    case 'm': countMale.setNum(countMale.getNum() + 1);
    sumMaleGPA.setNum(sumMaleGPA.getNum() + GPA);
    break;

    }
    }
    }




    public static void averageGrade(IntClass countFemale, IntClass countMale, DoubleClass sumFemaleGPA, DoubleClass sumMaleGPA, DoubleClass averageFemaleGPA,DoubleClass averageMaleGPA)
    {
    if (countFemale.getNum() != 0)
    averageFemaleGPA.setNum( sumFemaleGPA.getNum() / countFemale.getNum());

    if (countMale.getNum() != 0)
    averageMaleGPA.setNum( sumMaleGPA.getNum() / countMale.getNum());
    }



    public static void printResults(IntClass countFemale, IntClass countMale, DoubleClass sumFemaleGPA, DoubleClass sumMaleGPA, DoubleClass averageFemaleGPA, DoubleClass averageMaleGPA) throws FileNotFoundException
    {
    PrintWriter outFile = new PrintWriter("Ch7Ex12out.txt");

    outFile.printf("Stephen Bates");
    outFile.printf("Sum female GPA = %.2f %n",sumFemaleGPA);
    outFile.printf("Sum male GPA = %.2f %n",sumMaleGPA);
    outFile.printf("Female count = %d %n", countFemale);
    outFile.printf("Male count = %d %n", countMale);
    outFile.printf("Average female GPA = %.2f %n", averageFemaleGPA);
    outFile.printf("Average male GPA = %.2f %n", averageMaleGPA);
    outFile.close();
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Programming help

    Please use code tags when posting code.

    The problem is down to an extra opening curly brace between the class declaration and the main method, and presumably there's and extra closing curly brace at the end of the file.

  3. #3
    Join Date
    Nov 2009
    Posts
    2

    Re: Java Programming help

    Ok I tried what you suggested and I get a new error now.

    c:\solution\java>javac Ch7Ex12.java:98: reached end of file while parsing

    Im sorry if this seems like a newb mistake but I have been having issues all semster and just want to get it correct. Thanks

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Programming help

    Quote Originally Posted by lunchbox2626 View Post
    Ok I tried what you suggested and I get a new error now.

    c:\solution\java>javac Ch7Ex12.java:98: reached end of file while parsing
    This is the same kind of thing - your curly braces are not matched. The compiler fell off the end of your code looking for the closing brace.

    Hint: when opening curly braces, always put in the closing brace at the same time, then code between them. This way they will always match up.

    The sooner you start to code, the longer the program will take...
    R. Carlson
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2007
    Posts
    425

    Re: Java Programming help

    Quote Originally Posted by dlorde View Post
    Hint: when opening curly braces, configure your IDE to put in the closing brace at the same time, then code between them. This way they will always match up.
    Fixed.
    ------
    If you are satisfied with the responses, add to the user's rep!

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Programming help

    Quote Originally Posted by Deliverance View Post
    Fixed.
    Yup - I guess there's no good reason not to use a decent IDE, and if you're going to use one, make use of its facilities.

    The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities...
    E. Dijkstra
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured