0

How do I reinitialize array to null or zero when reprompted to renter data. Tried this but still computes average for previous entry.
Student[] students1 = new Student[studentCount];

here is a snippet of code

float totalscore = 0.0f;
		boolean isValid = false;
		String lastName = "";
		String firstName = "";
		String ID = "";
		int score = 0;

		while (!isValid)
		{
			System.out.print("\n   Enter number of students" );
			System.out.print("\n   to enter or \"0\" " + "<zero> to exit: " );
			if (sc.hasNextInt())	{	// make sure the next is an integer
				int studentCount = sc.nextInt();	// discard the entire line if not
				if (studentCount == 0)
				{
					System.out.printf("\n  Program terminated by User.\n");
					NasimDate.printfDate();
					NasimDate.printfTime();

					break;
				}
				else
				{
					isValid = false;
				}
				sc.nextLine();			// discard any remaining data
				// get the number of students and create the array
				//int studentCount = getStudentCount();
				Student[] students = new Student[studentCount];

				// get the data for each student and add a Student object to the array
				for (int i = 0; i < studentCount; i++)	{
					System.out.println();														// print a blank line
					lastName = getString("   Student " + (i+1) + "  last name: ");		// get last name
					firstName = getString("\t   " + (i+1) + " first name: ");			// get first name
					ID = getID("\t   " + (i+1) + " ID:    \t ");	// get ID
					score = getScore("\t   " + (i+1) + " score:  \t ");						// get score
					students[i] = new Student(lastName, firstName, ID, score);					// add to array
					totalscore += score;		// get total score
				}
				// get average grade
				float averageGrade = totalscore/studentCount;

				// sort the array
				Arrays.sort(students);

				System.out.println();
				System.out.println("   * Summary of Grades Entered * \n");

				System.out.printf("  %3s%15s%14s%4s",
				"ID",  "Last Name", "First Name", "  Score");

				// print the array
				System.out.println();
				for (Student s : students)		// for each Student in the array
					System.out.println("   " + s.getSummaryLine());
					// Average Grade
					System.out.println("\n   Average Grade  " + averageGrade);
				System.out.println();			// print the final blank line
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Chaosis
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.