I am given an integer, N, which is the number of test scores that will be inputted. For each line, N, there will be a student name followed their test score. I need to compute the sum of their test scores & print the the second-smallest student's name.
Asked
Active
Viewed 1,717 times
0
-
I see H's test score to be 4 in your input. – Gyanshu Jan 14 '17 at 21:08
-
There are students with duplicate scores and B has score 6 and E has score 3 (not 7). Also H has 4 – ucsunil Jan 14 '17 at 21:09
-
You must add them. It is the sum of their scores (see how H has 4 and then later it says H has 5). 4 + 5 = 9. – Jan 14 '17 at 21:11
-
I see the output should be Z which is 4 – Nishank Jan 14 '17 at 21:12
-
I fixed one mistake in the test data. – Jan 14 '17 at 21:13
-
All of the student test scores are summed for that individual student. I must print out the second-smallest student score. – Jan 14 '17 at 21:13
-
2Instead of adding somany if checks, directly take the student name as key and sum of their scores as values into Map. Then just sort the map by values and then take the second student name to show the output – Nishank Jan 14 '17 at 21:17
-
Hi, I think I did something similar to this but I am not getting anywhere. – Jan 14 '17 at 21:28
-
can you please show us your work – Nishank Jan 14 '17 at 21:35
-
yes I have updated the original post. – Jan 14 '17 at 21:42
-
Get rid of those case statements. As @Nishank hinted, use a map - https://www.tutorialspoint.com/java/java_map_interface.htm and http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java . – OldProgrammer Jan 14 '17 at 21:43
-
`Arrays.sort(students);` – Mohsen_Fatemi Jan 14 '17 at 21:57
1 Answers
0
So, what I would do is create an array of classes for the students. The class would have two instance variables for the name and score. Then when all the input is done, all you need to do is get them. Here is the code that I came up with for that exact thing.
import java.util.*;
public class testScores {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Student[] students = new Student[n];
for(int i = 0; i < n; i++){
students[i] = new Student();
System.out.print("Enter the student's name");
students[i].setName(scan.next());
scan.nextLine();
System.out.print("Enter the student's score");
students[i].setScore(scan.nextInt());
scan.nextLine();
}
int total = 0;
int smallest_name = 0;
for(int i = 0; i < n; i++){
total+=students[i].getScore();
if(students[i].getName().length() < students[smallest_name].getName().length())
smallest_name = i;
}
int second_smallest = 0;
for(int i = 0; i < n; i++){
if(students[i].getName().length() > students[smallest_name].getName().length() && students[i].getName().length() < students[second_smallest].getName().length())
second_smallest = i;
}
System.out.println("The sum of the scores is: " + total);
System.out.println("The second smallest name is: " + students[second_smallest].getName());
}
}
class Student{
private String name;
private int score;
public Student(){}
public void setScore(int n){
score = n;
}
public void setName(String n){
name = n;
}
public int getScore(){
return score;
}
public String getName(){
return name;
}
}

Josh Heaps
- 335
- 1
- 8