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: Help fixing my Java program! Reading files

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Help fixing my Java program! Reading files

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class EchoFileData
    {
        public static void main(String [] args) throws IOException
        {
            int grade;
            int count = 0;
            int total = 0;
            int maxGrade = 0;
            int minGrade = 0;
            int passed = 0;
            int gradeA = 0;
            int gradeB = 0;
            int gradeC = 0;
            int gradeD = 0;
            int gradeF = 0;
     
            File inputFile = new File( "Grades.txt" );
            Scanner scan = new Scanner( inputFile );
     
            maxGrade = scan.nextInt ();
            minGrade = scan.nextInt ();
     
            while ( scan.hasNext() )
            {
                grade = scan.nextInt();
     
                count++;
     
                total += grade;
     
     
                if(grade > maxGrade)
                    {
                        maxGrade = grade;
                    }
                if (grade < minGrade)
                    {
                        minGrade = grade;
                    }
                if (grade >= 60)
                    {
                        passed ++;
                    }
                if (grade >= 90)
                    {
                        gradeA++;
                    }
                if (grade >= 80 && grade < 90)
                    {
                        gradeB++;
                    }
                if (grade >= 70 && grade < 80)
                    {
                        gradeC++;
                    }
                if (grade >= 60 && grade < 70)
                    {
                        gradeD++;
                    }
                if (grade < 60)
                    {
                        gradeF++;
                    }
                }
     
            System.out.println("The total number of students in the class is " + count + ".");
     
            DecimalFormat decimalPlace = new DecimalFormat( "##.0" );
            System.out.println("The class average test score is " + decimalPlace.format((double) (total)/ count) + ".");
            System.out.println("The highest score is " + maxGrade + ".");
            System.out.println("The lowest score is " + minGrade + ".");
            System.out.println("The number of students who passed is " + passed + ".");
            System.out.println("The number of students who received an A is " + gradeA + ".");
            System.out.println("The number of students who received an B is " + gradeB + ".");
            System.out.println("The number of students who received an C is " + gradeC + ".");
            System.out.println("The number of students who received an D is " + gradeD + ".");
            System.out.println("The number of students who received an F is " + gradeF + ".");
        }
    }

    This is my code and I am importing a file and need to print out the number of students in the class, the average test score, the highest and lowest grade, and how many received each letter grade. I will attach the text file I imported.
    My output says there is only 33 students and there should be 35. It was outputtingGrades.txt 35 before I put in all the if statements to add to the number of each grade. Then it changed to 33 and I'm not sure how to fix it.


  2. #2
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help fixing my Java program! Reading files

    Your problem is here:
    maxGrade = scan.nextInt ();
    minGrade = scan.nextInt ();
    The counter is not increased for the first two lines you read. I would set the maxGrade to 0, and minGrade to 100, then remove those two lines.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help fixing my Java program! Reading files

    My output says there is only 33 students and there should be 35.
    Is the program not reading some lines from the file? Print out the lines as they are read so you can see which lines are not being read.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Help fixing my Java program! Reading files

    D

Similar Threads

  1. java GUI , reading in files
    By zerocool18 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 25th, 2013, 07:23 PM
  2. Fixing this grade finder program?
    By Stinger25 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 4th, 2012, 08:10 AM
  3. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 06:52 PM
  4. Reading CSV files into a program
    By Wrathgarr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2010, 10:41 AM
  5. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM