Help with searching through Arrays
Hey everybody I am looking for some help with this Java assignment I am currently working on. It is a program that is primarily using arrays. The user is asked to enter how many students there are. Then the program will ask the user for each students ID, first name, last name, and score. The ID, first name, last name, and score are all stored in separate arrays. After this the program will call on another method to determine the average score of the students. Here comes the hard part. Now I am have to ask the user to search the 'database' by entering the student ID. If the ID is valid, the program will display that students ID, first name, last name, and score. Also, if the score has the highest score it will say so. I am also suppose to display the students deviation from the score, but I am clueless as to how to do that. My main problem is I do not know how to let the user search the arrays by entering an ID. And the program has to keep asking for the user to enter an ID until the user enters "quit". Any help would be much appreciated. Thanks! Below is my code.
Code:
import java.util.Scanner;
import java.util.Arrays;
public class StudentArray
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many students are there? ");
int totalStudents = input.nextInt();
int[] studentID = new int[totalStudents];
String[] firstName = new String[totalStudents];
String[] lastName = new String[totalStudents];
double[] studentScore = new double[totalStudents];
for (int i = 0; i < totalStudents; i++)
{
System.out.printf("Enter the Student ID of Student %d: ", i + 1);
studentID[i] = input.nextInt();
System.out.printf("Enter the First Name of Student %d: ", i + 1);
firstName[i] = input.next();
System.out.printf("Enter the Last Name of Student %d: ", i + 1);
lastName[i] = input.next();
System.out.printf("Enter the Score of Student %d: ", i + 1);
studentScore[i] = input.nextDouble();
if (studentScore[i] < 1 || studentScore[i] > 100)
{
System.out.println("Please enter a number between 0 and 100: ");
studentScore[i] = input.nextDouble();
}
}
System.out.println("\n\nThe average score of the students is: " + calculateAverage(studentScore));
System.out.println("Search for a Student by entering their ID: ");
int k = input.nextInt();
//This is what I was attempting to do the search with
for (int i = 0; i < totalStudents; i++)
{
System.out.println("\n\nSearch for a Student by entering their ID: ");
int key = input.nextInt();
if (key == studentID[i])
{
System.out.println("Student ID: " + studentID[i] + "\nFirst Name: " + firstName[i] +
"\nLast Name: " + lastName[i] + "\nScore: " + studentScore[i]);
if (studentScore[i] == highestScore(studentScore))
System.out.println("This Student has the highest score!");
}
}
}
public static double calculateAverage(double[] studentScore)
{
double sum = 0;
for (int i = 0; i < studentScore.length; i++)
{
double value = studentScore[i];
sum += value;
}
double average = sum / studentScore.length;
return average;
}
public static double highestScore(double[] studentScore)
{
double highest = studentScore[0];
for (int i = 0; i < studentScore.length; i++)
{
if (studentScore[i] > highest) highest = studentScore[i];
}
return highest;
}
}
Re: Help with searching through Arrays
The most challenging part of this is the fact that you have multiple arrays for one type of data (do you need to store it in different arrays? Having just a single array will make this much easier). You must make sure that all arrays grow and shrink in unison (IE if bob is the third person in the name array, you must make sure the 3rd element in each other array corresponds to information about bob). There are many ways to do this of course, you can either keep track of the highest score while you build your 'database', or you can search for the highest score before finding the student you are looking for.
You have the highest score method, so after building your data, find and save the highest score. Then simply search through the ID array, you will be using a linear search I imagine since sorting multiple lists would be quite challenging. So some pseudo code for a linear search is shown below:
Code:
while current != key
continue through loop
end while
return current
This isn't the only way to do this, but the effect is to skip through the list until you find the actual search element (key).