Chris Wallace Chris Wallace - 2 months ago 19
Java Question

Java Homework issue with scanner input

Here is the professors prompt
:
Write a public class called Find2Max with a method called run with
the following header:

public void run()

The method run whould solve the following problem:


  1. prompt the user to enter the number of students and

  2. for each student prompt the user to enter the name and score

  3. when done reading data for all students, display the two students
    with the highests scores.



(notice that if the number of students is less than 2, the program
still reads two students)

And heres my code:

import java.util.Scanner;

public class Find2Max {

public void run() {

Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of at least 2 students: ");
int number_of_students = sc.nextInt();

if(number_of_students < 2){
number_of_students = 2;
}

do{

double top_grade = 0;
double second_top_grade = 0;
String top_kid = "";
String second_top_kid= "";

for(int ii = 0; ii < number_of_students; ii++){
System.out.print("Enter a student name: ");
String student_name = sc.nextLine();

System.out.print("Enter a student score: ");
Double student_grade = sc.nextDouble();

if(student_grade > top_grade){
top_kid = student_name;
top_grade = student_grade;
}
else if(student_grade > second_top_grade && student_grade < top_grade){
second_top_grade = student_grade;
second_top_kid = student_name;
}
else{
student_grade = 0.00;
student_name ="";
}
}
System.out.println("Top two students: ");
System.out.println( top_kid + "'s score is " + top_grade);
System.out.println(second_top_kid + "'s score is " + second_top_grade);
}while(number_of_students >= 2);
}

public static void main(String[] args){
Find2Max test = new Find2Max();
test.run();
}
}


I tried hard to solve the problem and Im really close but am confused why my program seems to skip over and not read my input for the Enter students name part. Also it seems to have this problem when the number of students is 1. Any help would be appreciated ! : ]

PNS PNS
Answer

The original code has numerous issues:

  • It does not handle well line transition
  • It does not handle the case where the second student grade is higher than the first
  • It does not handle the corner case of grade equality

Following is a slightly improved version, with some extra new lines to make the output more readable. A modification to allow the program to exit gracefully, is to exit if the number of students supplied is less than 2, instead of setting the number to 2 artificially. The 3rd else statement is commented out, as not needed.

public class Find2Max {

  public void run() {
    Scanner sc = new Scanner(System.in);
    System.out.print("\nEnter the number of students (at least 2 required for comparison): ");
    int number_of_students = sc.nextInt();

    if(number_of_students < 2){
      System.out.println("\nThis program compares scores for 2 or more students.");
      // number_of_students = 2;
      return;
    }

    do{

      double top_grade = 0;
      double second_top_grade = 0;
      String top_kid = "";
      String second_top_kid= "";

      for(int ii = 0; ii < number_of_students; ii++){
        System.out.print("\nEnter a student name: ");
        String student_name = sc.next();

        System.out.print("Enter a student score: ");
        Double student_grade = sc.nextDouble();

        if(student_grade >= top_grade){
          if(top_grade >= second_top_grade) { // Save this grade as the second top
            second_top_kid = top_kid;
            second_top_grade = top_grade;
          }
          top_kid = student_name;
          top_grade = student_grade;
        }
        else if(student_grade > second_top_grade && student_grade < top_grade){
          second_top_grade = student_grade;
          second_top_kid = student_name;
        }
//        else{
//          student_grade = 0.00;
//          student_name ="";
//        }
      }
      System.out.println("\nTop two students: ");
      System.out.println( top_kid + "'s score is " + top_grade);
      System.out.println(second_top_kid + "'s score is " + second_top_grade);
    }while(number_of_students >= 2);
  }

  public static void main(String[] args){
    Find2Max test = new Find2Max();
    test.run();
  }
}
Comments