Results 1 to 12 of 12
  1. #1
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default Test score average

    I have to write a program that reads a student's name together with his test scores. Then i have to compute the average for each student. I must make a void method to calculate the average then a value returning method to determine the students grade. How do i combine the name with the average when reading the scores from a text file?

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

    Default

    Much depends on how the data is laid out in the text file. That will determine how you read in the data. next you probably want to create a Student class that holds the name String and the numeric data that is read in for each student.

  3. #3
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    each line is the last name then five scores, such as:
    Johnson 85 76 88 90 67

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    SO you have a line of text simply. Need to separate them each depends on the space character within each two segments.

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

    Default

    I would use a Scanner object to read in each line of the file and put it into a String. I'd then call split(" ") on the same String to split it into a array of 6 small Strings (tokens), the name and the 5 test scores. I'd then create a new Student object and place this data, the name String, and the parsed int scores into the new student object, then place this student object into a collection such as an arraylist,.....

  6. #6
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    the problem is i havent learned split yet but we have just learned StringBuffer. Would i be able to use that some how?

  7. #7
    ryn21 is offline Member
    Join Date
    Oct 2008
    Posts
    16
    Rep Power
    0

    Default

    im so lost right now, trying to learn this chapter on methods and write this program is starting to feel hopeless

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Are you not allowed to independently look up methods to use?

    If you can't use split(), then I'm assuming you can't use charAt() (since they're both methods of String), which pretty much means you can't do your project.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Can you show your code, what you have done so far. The way Fubarable explain is the most perfect way for me.

  10. #10
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    I would use a Scanner object to read in each line of the file and put it into a String. I'd then call split(" ") on the same String to split it into a array of 6 small Strings (tokens), the name and the 5 test scores. I'd then create a new Student object and place this data, the name String, and the parsed int scores into the new student object, then place this student object into a collection such as an arraylist,.....
    Or you read the data from the file (I prefer a (Buffered) FileReader) and when you read 1 line, you try to parse it as an int. If an Exception is thrown you know that's the name of the student. Than you can read the remaining lines and put them in an array.
    I die a little on the inside...
    Every time I get shot.

  11. #11
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    sir.. do you have any codes for this.. because we have the same problem..:confused::confused: please?

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Supamagier View Post
    Or you read the data from the file (I prefer a (Buffered) FileReader) and when you read 1 line, you try to parse it as an int. If an Exception is thrown you know that's the name of the student. Than you can read the remaining lines and put them in an array.
    But the reason is on a single line all the details are included, like name and scores. So you need to split them.

Similar Threads

  1. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM
  2. Replies: 0
    Last Post: 04-05-2008, 12:38 AM
  3. show a high score from tetris
    By stessie in forum Java Applets
    Replies: 0
    Last Post: 03-19-2008, 03:00 AM
  4. get the average and maximum score
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 04:15 AM
  5. Klaverjas Score 0.2
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-23-2007, 07:40 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
  •