Results 1 to 2 of 2
  1. #1
    frankied22 is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default 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.

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    public class StudentArray
    	public static void main(String[] args) 
    		Scanner input = new Scanner(;
    		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] =;
    			System.out.printf("Enter the Last Name of Student %d: ", i + 1);
    			lastName[i] =;
    			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;

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power

    Default 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:

    Java 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).

Similar Threads

  1. Java Arrays( Searching ) Please help.
    By Mogmash in forum New To Java
    Replies: 4
    Last Post: 11-14-2012, 06:02 PM
  2. File to Array Writing and Searching Arrays
    By michaelJava in forum New To Java
    Replies: 0
    Last Post: 02-13-2012, 11:35 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Searching arrays
    By drymsza1234 in forum New To Java
    Replies: 5
    Last Post: 12-03-2009, 06:23 PM
  5. Searching Arrays
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 07-26-2008, 06:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts