Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Averages, Highest & using a .csv file ! ? ! ?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Averages, Highest & using a .csv file ! ? ! ?

    Please Help me if you can. I'm doing a project that is supposed to output Averages on student Test Scores (5 Students), and output their names, as well as display the highest score. The student score are read through an excel .csv file.

    Here is the two files that I have come up with this far.

    TestscoreReader.java
     
     
        package assignment;
     
        //****************************************************
        //The TestScoreReader class reads test scores as    *
        //tokens from a file and calculates the average     *
        //of each line of scores.                           *
        //****************************************************
     
        import java.io.*;
        //import java.util.StringTokenizer;
        import java.util.Scanner;
     
        public class TestScoreReader
        {
     
        private Scanner inputFile;
        private String line;
     
        //*************************************************   
        // The constructor opens a file to read the grades*
        // from                                           *
        //*************************************************
     
        public TestScoreReader(String filename)throws IOException
        {
           File file = new File(filename);
           inputFile = new Scanner(file);
        }
     
        //**************************************************  
        // The readNextLine method reads the next line     *
        // from the file. If a line was read, the method   *
        // returns true. Otherwise it returns false.       *
        //**************************************************
     
        public boolean readNextLine() throws IOException
        {
           boolean lineRead; // Flag indicating successful read
     
     
           // Determine whether there is more to read.
           lineRead = inputFile.hasNext();
           // if so, read the next line.
           if (lineRead)
               line = inputFile.nextLine();
     
           // Return the status of the read operation.
           return lineRead;
        }
     
        //**************************************************  
        // The getAverage method returns the average of    *
        // last set of test scores read from the file.     *
        //**************************************************
     
        public double getAverage()
        {
           int total = 0;    // Accumulator
           double average;   // To hold the average test score
     
           //Tokenize the last line read from the file.
           String[] tokens = line.split(",");
     
           // Calculate the total of the test scores
     
           for (String str : tokens)
           {
              total += Integer.parseInt(str);
           }
     
           // Calculate the average of the test scores.
           // Use a cast to avoid integer division.
           average = (double) total / tokens.length;
     
           // Return the average.
           return average;
        }
     
        //**************************************************  
        // The close method closes the file.               *
        //**************************************************
     
        public void close() throws IOException
        {
           inputFile.close();
        }
        }

    and...

    Testaverages.java


        package assignment;
     
        //********************************************************
        //This program uses the TestScoreReader class to read   *
        //test scores from a file and get their averages.        *
        //********************************************************
     
        import java.io.*;    
     
        public class TestAverages
        {
        public static void main(String[] args)
                           throws IOException
        {
           double average;           // To hold an average
           int studentNumber = 1;    // To count students
     
           // Create a TestScoreReader object.
           TestScoreReader scoreReader =
                    new TestScoreReader("/Users/cameroncashwell/Documents/Grades.csv");
     
           // Process the file contents.
           while (scoreReader.readNextLine())
           {
              // Get this student's average.
              average = scoreReader.getAverage();
     
              // Display this student's average.
              System.out.println("Average for student number "
                                 + studentNumber + " is "
                                 + average);
     
              // Increment the student number.
              studentNumber++;
           }
     
           // Close the file.
           scoreReader.close();
           System.out.println("No more scores.");
        }
        }

    Here is my compiled errors.
    "Exception in thread "main" java.lang.NumberFormatException: For input string: "Jane Doe"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at assignment.TestScoreReader.getAverage(TestScoreRea der.java:68)
    at assignment.TestAverages.main(TestAverages.java:26) "


    So I think I'm understanding how to display the average. Still trying to figure out how to display highest score.. And trying to figure out why it's getting an error with the value of the .csv file.

    Not asking anyone to do this for me, just a point toward tutorials, advice, something to work with or any help you can give. Not too great at Programming so will probably be up all night with this thing.

    Thanks in advance.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Averages, Highest & using a .csv file ! ? ! ?

    Hello thebigtimeGNAR.

    Welcome to the Java Programming Forums

    Can you please attach your .csv file? I will then attempt to compile your code...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Averages, Highest & using a .csv file ! ? ! ?

    It's telling me the file is Invalid to upload?

    but it consists of a simple spreadsheet similar to,

    "Jane Doe, 86, 88, 77, 92, 99"
    "Another Person, 88, 33, 99, 100"

    and 3 others similar.

    So, I'm understanding how to pull the numeric values from the .csv file, but am unable to figure out a way to pull the names to output with the averages. I'm trying to use getName & char but not figuring out how to finish through with them, declare them etc. Am I going the right route?

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Averages, Highest & using a .csv file ! ? ! ?

    Also, I'm supposed to output 'the highest score' as well. So i'm thinking throwing something like this in there-

    public String getName(){ 079  String name = file.getName(); 080  if (ref != null && !"".equals(ref)){ 081  name += "#" + ref;

Similar Threads

  1. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  2. How to change File Type of a File Using java
    By akash169 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 31st, 2010, 02:58 AM
  3. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  4. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM

Tags for this Thread