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: Reading text files and the calculating averages from it

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading text files and the calculating averages from it

    Ok so i am working on an assingment where I have to read data from text file and the calculate the average of both classes in each major. There are two majors CS and Math and two scores listed for each major - the Math class score and the Java class score.

    I got to the point where it reads every line of the data.txt file and the prints the data neatly by line.

    Where i am stuck is , I have to figure out the averages of each Major and the classes seperately for each major.

    the data.txt looks like this.

    John Smith
    CS
    89
    99
    Jonathan Sou
    MATH
    78
    98
    James Scott
    CS
    78
    87
    Adam Brown
    MATH
    99
    59
    Peter Paulson
    CS
    78
    100

    here is my current code so far.
     
    import java.io.File;       // for File
    import java.io.FileNotFoundException;
    import java.util.Scanner;  // for Scanner
    public class TEST 
    {
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		Scanner input = new Scanner(new File("data.txt"));
     
    		String firstname, lastname;
    		String major;
     
    		while (input.hasNext()) 
    		{
    				firstname = input.next(); 
    				lastname = input.next(); 
    				major = input.next();
    				double math = input.nextDouble(); 
    				double java = input.nextDouble(); 
    				System.out.println(firstname + " " + lastname + ", " + " " + major + ", " +  math + ", " + java); 
    		}
     
    		input.close();				
    	}
    }
    CURRENT OUTPUT

    John Smith, CS, 89.0, 99.0
    Jonathan Sou, MATH, 78.0, 98.0
    James Scott, CS, 78.0, 87.0
    Adam Brown, MATH, 99.0, 59.0
    Peter Paulson, CS, 78.0, 100.0

    WHAT I NEED OUTPUT TO BE

    John Smith, CS, 89.0, 99.0
    Jonathan Sou, MATH, 78.0, 98.0
    James Scott, CS, 78.0, 87.0
    Adam Brown, MATH, 99.0, 59.0
    Peter Paulson, CS, 78.0, 100.0
    ----- ---------- ----------
    MATH Math AVG= 88.5 Java AVG= 78.5
    CS Math AVG= 81.66666666666667 Java AVG= 95.33333333333333

    Can someone point on the right path?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Reading text files and the calculating averages from it

    Please post the output from the program.

    Do you know how to add the value of one variable to another? Use that technique to add up the values and to count the number of values that were read.

    After all the lines have been read, compute the averages and print them.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading text files and the calculating averages from it

    I had the output listed. I have edited to wrap the code. thanks

    Where I am having issues wrapping my head around, is how do i tell it to look and add the scores that are listed on the lines for each major. Like how could i tell it to sum up and average the line that lists scores for CS Major and line that lists scores for the Math Major.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Reading text files and the calculating averages from it

    scores that are listed on the lines
    The scores are in the variables that were read.

    Do you know how to add the value of one variable to another?
    total = total + detail; // add detail to the total
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem reading in the files
    By Skynet928 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 28th, 2013, 01:05 PM
  2. Reading Text Files Assignment [Scanner Method]
    By iAce in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 9th, 2012, 09:41 PM
  3. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 2nd, 2012, 11:13 PM
  4. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 11:47 AM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:16 PM

Tags for this Thread