Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Introduction to programing class. I am stuck on my assignment arrays.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Introduction to programing class. I am stuck on my assignment arrays.

    I have an array assignment that I am stuck on. This is the assignment. This is my first time posting here, but I have gained alot of information from the other posts.

    Create a program that asks the user how many students there are. Then, for each student, ask for a student id, the first name, last name, and score (must be between 0 and 100 to be valid). When all the data has been entered, calculate and display the average. Allow the user to search for a student by entering a student id. If the student id is valid and a student is found, display the student's full name, score, and deviation of the score from the average. If the student has the highest score, display a message indicating that. Continue asking the user to search for students until the user enters "quit", at which point the program should exit.

    The following is the code I have so far. I am not sure how to search the arrays and also how to return all the information from the different arrays.

    import java.util.Scanner;
     
    public class Assn06 {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    // User input number of students	
    		System.out.print("Enter the number of students");
    		int studentNumber=input.nextInt();
     
    		int sum = 0;
     
    // Array for student ID
    		int[] studentid = new int[studentNumber];
    		String[] firstname = new String[studentNumber];
    		String[] lastname = new String[studentNumber];
    		int[] studentscore = new int[studentNumber];
    		int i;
    		for ( i = 0 ; i < studentNumber ; i++)
    		{
    			studentscore[i] = studentid[i];
    			firstname[i] = lastname[i];
    			System.out.printf("Enter student %d ID : ", i+1);
    			int studentId = input.nextInt();			
    			studentid[i] = studentId;
     
    			System.out.printf("Enter student %d first name : ", i+1);
    			String studentFirstname = input.next();
    			firstname[i] = studentFirstname;
     
    			System.out.printf("Enter student %d last name : ", i+1);
    			String studentLastname = input.next();
    			lastname[i] = studentLastname;		
     
    			System.out.printf("Enter student %d score : ", i+1);
    			int studentScore = input.nextInt();
    			studentid[i] = studentScore;
    			sum += studentScore;		
    		}	
     
    //Calculate Average
    		int average = sum / studentNumber;
     
    //Display Average
    		System.out.println("The student average is " + average);
     
    // Search by student Id
     
    		System.out.printf("Search for student by entering ID: ");
    		int studentIdsearch = input.nextInt();
     
     
     public static int linearSearch(studentid, studentIdsearch)
    {
     
    	for (int j = 0 ; j < list.length; j++)
    	{
    		if (studentIdsearch == list[j])
    			return j;
    	}
    }
     
     
     
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Introduction to programing class. I am stuck on my assignment arrays.

    Hi. I understand you are in an intro class, and while your implementation will work for the purpose of this assignment, have you thought about using a separate class to hold all the information for a student? If you do not know what I am talking about, just forget everything I have said and read on..

    You are off to a good start. You have this:
    public static int linearSearch(studentid, studentIdsearch)
    {
     
    	for (int j = 0 ; j < list.length; j++)
    	{
    		if (studentIdsearch == list[j])
    			return j;
    	}
    }
    Let us utilize this method for search. Start with the most basic criteria: Allow the user to search for a student by entering a student id. First, which array would you use to search for IDs? The array that holds IDs, of course: studentid! Now, how would you use that for-loop with studentid[]? When do you stop searching? What do you do when you find the ID? Utilize the linearSearch(.., ...) method -- it is all there for you; you just need to fill everything in!

Similar Threads

  1. Need help with a Java Assignment regarding enums and arrays
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2012, 07:49 AM
  2. [SOLVED] A little assignment involving arrays.
    By Melawe in forum What's Wrong With My Code?
    Replies: 39
    Last Post: May 1st, 2011, 10:43 PM
  3. help with a programing assignment.
    By oscar_m in forum Object Oriented Programming
    Replies: 4
    Last Post: May 1st, 2011, 02:00 PM
  4. [SOLVED] stuck on an assignment and need help with repetition
    By teeej86 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 20th, 2011, 07:03 PM
  5. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM