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 3 of 3

Thread: Print Student average, high, and low grades through array?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print Student average, high, and low grades through array?

    So I need to make my program print the total number of students, the average grade, and both the high and low grade through the array which I created through the text file attached. Can someone help me understand how to do this? What I have so far is below:

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

    public class Student
    {
    String fname, lname, number, status;
    int grade;


    public Student(String fname, String lname, int grade)
    {
    this.fname = fname;
    this.lname = lname;
    this.grade = grade;
    this.status = getStatus(grade);

    }

    public String getStatus(int grade)
    {
    String status = null;
    if (grade > 89)
    status = "Excellent";

    if (grade >= 60 && grade <= 89)
    status = "Okay";

    if (grade < 60)
    status = "Failure";

    return status;
    }
    public String toString(){
    return fname + " " + lname + " " + grade;
    }
    }




    import java.util.Scanner; //imports Java Scanner
    import java.io.*;//imports File Scanner
    public class Grades //Name of Class

    {
    public static void main (String[] args) throws IOException
    { String first_name, last_name;
    int grade = 0, total=0, count=0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
    int arrCtr = 0;

    Student st[] = new Student[4] ;


    double average = 0;
    Scanner fileInput = new Scanner(new File("students.txt"));
    while (fileInput.hasNext())//scans file
    {
    first_name = fileInput.next();
    last_name = fileInput.next();
    grade = fileInput.nextInt();
    st[arrCtr] = new Student(first_name,last_name,grade);
    arrCtr = arrCtr + 1;
    }
    count = arrCtr + 1;
    System.out.println ("Students with excellent grades");
    for(int i=0; i<arrCtr; i++)
    {


    if (st[i].status == "Excellent") {
    System.out.println(st[i].toString() );

    }
    } // end for
    System.out.println ("Students with okay grades");
    for(int i=0; i<arrCtr; i++)
    {


    if (st[i].status == "Okay")

    System.out.println(st[i].toString() );

    }
    System.out.println ("Students with failing grades");
    for(int i=0; i<arrCtr; i++){



    if (st[i].status == "Failure")

    System.out.println(st[i].toString() );

    }





    }
    }

    Any help would be greatly appreciated.
    Attached Files Attached Files


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Print Student average, high, and low grades through array?

    What specific problems do you have? Ask a specific question get a specific answer.

    BTW do not compare Strings (or other objects) with ==, use the equals method instead.
    Improving the world one idiot at a time!

  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: Print Student average, high, and low grades through array?

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

Similar Threads

  1. Print array with commas
    By GoPro in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2012, 12:41 AM
  2. Students/Grades
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 12th, 2011, 10:42 AM
  3. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  4. Calculating average of an array
    By Vika in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 29th, 2011, 08:06 AM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM