Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:30:43+00:00 2026-06-16T10:30:43+00:00

I’m having a bit of trouble formatting my code to give the correct output.

  • 0

I’m having a bit of trouble formatting my code to give the correct output. I’m a beginner at Java, so I expect I’ve made some rather stupid mistakes, but here goes.

The text file that I’m inputting is:

John Smith  90
Barack Obama    95
Al Clark    80
Sue Taylor  55
Ann Miller  75
George Bush 58
John Miller 65

The output file looks a little something like this:

John Smith  90
Barack Obama    95
Al Clark    80
Sue Taylor  55
Ann Miller  75
George Bush 58
John Miller 65

Students with excellent grades:
John Smith  90
Barack Obama    95

Students with ok grades:
Al Clark    80
Ann Miller  75
John Miller 65

Students with failure grades:
Sue Taylor    55
George Bush  58


Lowest Grade: Sue Taylor 55 
Highest Grade: Barack Obama 95
Average of Grades: 74


Grades in descending order: 
John Smith 55 
Barack Obama    58 
Al Clark    65 
Sue Taylor  75 
Ann Miller  80 
George Bush 90 
John Miller 95 

The output that I’m looking for is this:

The output file looks a little something like this:

John Smith  90
Barack Obama 95
Al Clark 80
Sue Taylor   55
Ann Miller   75
George Bush  58
John Miller  65


Students with excellent grades:
John Smith  90
Barack Obama 95


Students with ok grades:
Al Clark   80
Ann Miller   75
John Miller  65


Students with failure grades:
Sue Taylor    55
George Bush  58


Lowest Grade: Sue Taylor 55
Highest Grade: Barack Obama 95
Average of Grades: 74


Grades in descending order: 
Barack Obama 95
John Smith 90
Al Clark 80
Ann Miller 75
John Miller 65
George Bush 58
Sue Taylor 55

The issues are:

  1. I need to have the names (first and last) print with the
    grades for the minimum and maximum.

  2. The descending order of the grades is obviously ascending, so that
    needs to be fixed.

  3. I also need the names (first and last) to print with each grade in
    the organized descending list at the end, right now the names don’t match up to the grades.


Here’s my code:

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

public class Students
{
public static void main (String[] args) throws IOException
{   
    String first_name, last_name;
    int grade, total=0, count=0;
    int min, max;

    double average;

     int excellentTotal = 0;
     int okTotal = 0;
     int failureTotal = 0;

    Scanner fileInput = new Scanner(new File("students.txt"));
    Student st[] = new Student [100];
    while (fileInput.hasNext())
    {
        first_name = fileInput.next();
        last_name = fileInput.next();
        grade = fileInput.nextInt();    
        st[count] = new Student(first_name, last_name, grade);
        count++;
        total = total + grade; 
    }

    for (int i=0; i<count;i++)
    {
        System.out.println(st[i]);
    }

    System.out.println("\nStudents with excellent grades:");
    for (int i=0; i<count;i++)
    {
        if (st[i].grade > 89)
        {
            System.out.println(st[i]);
            excellentTotal += st[i].grade;
        }
    }

    System.out.println("\nStudents with ok grades:");
    for (int i=0; i<count;i++)
    {
        if (st[i].grade >=60 && st[i].grade <=89)
        {
            System.out.println(st[i]);
            okTotal += st[i].grade;
        }
    }

    System.out.println("\nStudents with failure grades:");
    for (int i=0; i<count;i++)
    {
        if (st[i].grade < 60)
        {
            System.out.println(st[i]);
            failureTotal += st[i].grade;
        }
    }       


    min = st[0].getGrade();
    max = st[0].getGrade();
    for (int i = 0; i < count; i++) 
    {
        if (max.grade < st[i].grade)
        {
            max = st[i];
        }
        if (min.st[i] > st[i].grade)
        {
            min = st[i].grade;
        }
        total = excellentTotal + okTotal + failureTotal;
    }

    System.out.println();
    System.out.println("Lowest Grade: " + min);
    System.out.println("Highest Grade: " + max);
    System.out.println("Average of Grades: " + total/count);

    int t, swap = 0;
    do 
    { 
        swap = 0; 
        for (int i=0; i<count-1; i++) 
        {
            if (st[i].getGrade()>st[i+1].getGrade()) 
            {
                t=st[i].grade; 
                st[i].grade=st[i+1].grade; 
                st[i+1].grade=t; 
                swap++;
            }
        } 
    }
    while (swap>0);

    System.out.println("\nGrades in descending order: ");

    for (int i=0; i<count;i++)
    {
         System.out.print (st[i] + " ");
         System.out.println ();
    }
}
static class Student
{
    private String fname, lname;
    private int grade;

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

    public int getGrade() 
    {
        return grade;
    }

    public void setGrade(int grade) 
    {
        this.grade = grade;
    }


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


}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:30:44+00:00Added an answer on June 16, 2026 at 10:30 am

    The issues are:

    1. I need to have the names (first and last) print with the grades for
      the minimum and maximum.

    Keep two Strings that each contain the name of the person with the min and max grade, just like you did with the actual min and max value.

    1. The descending order of the grades is obviously ascending, so that
      needs to be fixed.

    It looks like you’ve already sorted your array so that it is in ascending order (Btw look in to Arrays.sort() if you’re allowed), so instead of printing it from front to back, why don’t you print it starting from the end of your array? This should print it in descending order.

    1. I also need the names (first and last) to print with each grade in the
      organized descending list at the end.

    Your grades aren’t matching up with the correct person because you’re switching the grades of each person in the array, instead of switching the People around in the array.

    See here:

    t=st[i].grade; 
    st[i].grade=st[i+1].grade; 
    st[i+1].grade=t;
    

    Let’s pretend you have 2 people, Bob and Sam, with grades of 50 and 75,respectively.

    Here they are in the array: [Bob(50)][Sam(75)]

    Using the above code to swap them, you would end up with this: [Bob(75)][Sam(50)]

    The changes to fix this are trivial. I’ll leave it to you to figure out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.