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 8327683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:11:58+00:00 2026-06-09T01:11:58+00:00

I have a simple question about my code. I’m quite new to java and

  • 0

I have a simple question about my code. I’m quite new to java and trying to self learn but im kind of stuck on loops right now. To me it seems like this should work. The problem is too ask for a number of students, then have the user input the names and scores of each student. Then it should display the first and second highest scoring students. For some reason my code just shows the first name and score I enter for both the first highest score and the second highest score. I probably made some big mistake but maybe somebody can point me in the right direction? Sorry if this looks like a huge mess. 🙁

public class Chapter4_9 {

  public static void main(String[] args) {

    //scanner for input
    Scanner input = new Scanner(System.in);

    //ask user for number of students
    System.out.print("Enter the number of students: ");
    int numberStudents = input.nextInt();

    //declare variables
    double highestScore = 0;
    double tempScore = 0;
    double secondHighestScore = 0;
    String firstStudent = "";
    String tempStudent = "";
    String secondStudent = "";

    for (int i = 0; numberStudents != i; ++i) {
        System.out.print("Enter the students name followed by his score: ");
        String studentName = input.next();
        double studentScore = input.nextDouble();

        if (i == 0){
            firstStudent = studentName;
            highestScore = studentScore;
        }

        else if (studentScore > highestScore) {
            tempStudent = firstStudent;
            studentName = firstStudent;
            secondStudent = tempStudent;
            tempScore = highestScore;
            studentScore = highestScore;
            secondHighestScore = tempScore;
        }


    }   
    System.out.println("The highest scoring student is " + firstStudent + " with a " + highestScore);
    System.out.println("The second highest scoring student is " + secondStudent + " with a " + secondHighestScore);

}
}
  • 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-09T01:12:00+00:00Added an answer on June 9, 2026 at 1:12 am

    This block seems a little muddled:

    else if (studentScore > highestScore) {
        tempStudent = firstStudent;
        studentName = firstStudent;
        secondStudent = tempStudent;
        tempScore = highestScore;
        studentScore = highestScore;
        secondHighestScore = tempScore;
    }
    

    What is the intended consequence of this block? Why are you overwriting the value of studentName and studentScore, when they’re never read again (before you read new values from the user anyway)?

    Presumably the aim is to replace the second score/name with the highest score/name, and then replace the highest ones with the current input – but that’s not what the code does at all. This would do it:

    secondStudent = firstStudent;
    secondScore = highestScore;
    firstStudent = studentName;
    highestScore = studentScore;
    

    No need for temporary variables at all.

    However, just that change isn’t enough. You also need to consider the situation where the new score isn’t higher than the current highest score, but is higher than the current second highest score. I’ll leave you to work out what that requires…

    By the way, your code would probably be simpler if you introduced a separate class for the “name/score” combination, e.g. Student. Then you wouldn’t have parallel variables – you’d just have topStudent, secondStudent, currentStudent to worry about.

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

Sidebar

Related Questions

I have another simple question about Node. I'm trying to make a simple http
I have a fairly simple question about these 2 templating engines. I'm trying to
I have a simple question, but I'm about 80% sure that the answer to
Next question about russian encoding, mssql and python. I have this simple code: import
I have a question about @helper on Razor. I'm trying do a @helper simple
I have simple question about .net garbage collection. In the following code I create
I have what seems to be a simple question but its killing me trying
I'm new to Objective C and I have a simple question about memory management.
I'm a newbie about ASP.NET MVC 3, but I have a simple question. Is
I guess i have a simple question about better code organization. Say i have

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.