'Questions about one dimensional arrays and sorting in Java
I'm currently in the second half of a computer science class but I wasn't able to take the second one right after the first one. So I've forgot a large portion of my coding knowledge. I'm trying to re learn it all but it's quite tough without just going all the way back to the basics.
An assignment I'm working on currently is asking me to do the following:
Design a solution that requests and receives student names and an exam score for each. Use one-dimensional arrays to solve this. The program should continue to accept names and scores until the user inputs a student whose name is “alldone”. After the inputs are complete determine which student has the highest score and display that student’s name and score. Finally sort the list of names and corresponding scores in ascending order.
I wasn't really sure how to organize and start this but I went ahead and just started writing some code to at least make some progress and maybe figure something out.
The way I've set up my program doesn't exactly respond to the prompt as I was experimenting with array lengths and for loops to get reacquainted.
SO MY QUESTION IS, How would I make it so when users type "alldone" the program stops taking inputs and calculates the highest grade with the person who had it only using 1D Arrays.
Is it possible to take the names as a string and then insert the strings into an array?
Here is my code so far, It's not what the assignment is looking for but I just want to get some inspiration on some things I could do:
public static void main(String[] args) {
int studentcount;
Scanner input = new Scanner(System.in);
Scanner sc= new Scanner(System.in);
//Input to set array length
System.out.println("How many students are you grading? ");
studentcount=sc.nextInt();
//One dimensional array to hold student names/grades
String [] names = new String [studentcount+1];
int [] grades = new int [studentcount];
//Input to record student names/grades
System.out.println("Please enter the name of the students ");
for (int i = 0; i < names.length; i++) {
names[i] = sc.nextLine();
}
System.out.println("Please enter the grades of the students ");
for (int i = 0; i < grades.length; i++) {
grades[i] = sc.nextInt();
}
// For loop to display student names and grades
for (int i = 0; i < studentcount; i++) {
System.out.println(names[i+1] + " " + grades[i]);
}}}
The output looks like this:
How many students are you grading?
3
Please enter the name of the students
John
Brett
Wendy
Please enter the grades of the students
10
20
30
John 10
Brett 20
Wendy 30
Solution 1:[1]
You should create a class and implement the Comparable interface like this. The way I have it set up here, it sorts by name. If you want to sort by the score, just comment out the name compare and uncomment the grade compare.
public class Student implements Comparable<Student> {
private String name;
private int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
@Override
public int compareTo(Student o) {
//sorting by name
return name.compareTo(o.name);
//Compare by grade -- reversed so it's highest first
//return Integer.compare(o.grade, grade);
}
@Override
public String toString() {
return "Student [name=" + name + ", grade=" + grade + "]";
}
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
Student [] students = new Student[0];
while(true) {
System.out.println("Please enter the name of the student or \"alldone\" if complete: ");
String name = input.next();
if (name.contentEquals("alldone")) {
break;
}
System.out.println(String.format("Please enter %s's grade: ", name));
int grade = input.nextInt();
input.reset();
students = Arrays.copyOf(students, students.length+1);
students[students.length-1] = new Student(name, grade);
}
Arrays.sort(students);
for (Student student : students) {
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ryan |
