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 4 of 4

Thread: Grade Array Trouble

  1. #1
    Junior Member kite98765's Avatar
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Grade Array Trouble

    Hello there,

    I am making a two-dimensional array where you can input a maximum of 20 students and 7 grades, and am printing them out in a formatted table. The array calls for averaging each test's grades each student, giving an average of what the whole class scored on the test, needed to be printed bellow each column correctly. Then I must also be able to add information to the existing array, like adding more students. So far, I have gotten the enter of the information into the table format correct, but I can't find a way to do the last two requirements. I think I was on the verge of it, but it just errors.

    Here's what I have:
    import java.util.*;
    public class FormattedGradeArray {
     
    		/**
    		 * @param args
    		 */
    		public static void main(String[] args) {
    			// TODO Auto-generated method stub
    			Scanner indata = new Scanner(System.in);
    			// Establishes the rows and columns in the arrays,
    			// along with the average grades.
    			int students = 0, tests = 0;
    			double sum = 0;
    			// Establishes an array for student names, test grades and averages, and
    			// the final letter grade.
    			double[][] grade = new double[20][8];
    			String[] student = new String[20];
    			String[] letter = new String[20];
    			// This allows to set the number of students and test grades that you can enter.
    			System.out.println("Please enter the number of students you want to enter(1-20): ");
    			students = indata.nextInt();
    			System.out.println("Please enter the number of test grades (3-7): ");
    			tests = indata.nextInt();
     
    			System.out.println();
    			// This allows you to enter the student name, and their test grades.
    			for( int x = 0; x < students; x++){
    				System.out.println("Student Name: ");
    				student[x] = indata.next();
     
    				for(int y = 0; y < tests; y++){
    					System.out.println("Test Grade " +(y + 1) +": ");
    					grade[x][y] = indata.nextDouble();
    					sum += grade[x][y];
    				}
    				// Determines grade letter from the average score of each student.
    				grade[x][tests] = sum/tests;
    				if((grade[x][tests] <= 100) && (grade[x][tests] >= 89.5)){
    					letter[x] = "A";}
    				else if((grade[x][tests] <=89.5) && (grade[x][tests] >= 79.5)){
    					letter[x] = "B";}
    				else if((grade[x][tests] <= 79.5) && (grade[x][tests] >= 69.5)){
    					letter[x] = "C";}
    				else if((grade[x][tests] <= 69.5) && (grade[x][tests] >= 59.5)){
    					letter[x] = "D";}
    				else if((grade[x][tests] <= 59.5) && (grade[x][tests] >= 0)){
    					letter[x] = "F";}
     
    				sum = 0;
    				System.out.println();
    				}
    				// Output
    				System.out.printf("%-10s","Student Name" + "\t");
    				for(int y = 0; y < tests; y++){
    					System.out.printf("%-10s", "Test " + (y + 1));
    				}
    				System.out.println("	Average		Letter");
    				for(int q = 0; q < students; q++){
    					System.out.printf("%-12s", student[q]);
    					for(int w = 0; w < tests; w++){
    						System.out.printf("%10.2f", grade[q][w]);
    					}
    					System.out.printf("%12.2f", grade[q][tests]);
    					System.out.printf("%13s", letter[q]);
    					System.out.println();
    				}
    		}
    	}

    Thank you in advance for your suggestions.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Grade Array Trouble

    Are you limited to using standard Java arrays? If not, I'd suggest using the ArrayList class. It can auto-expand as you add elements beyond it's capacity. If not, then you have to do it the manual way:

    1. See if you're at the capacity of the array. If not, add it.
    2. If you are at the limit, create a new array with double the size (note: don't put it into the same variable as the old one or you'll lose the variable reference!).
    3. Copy over all the elements of the previous array
    4. Set the old variable reference to the new larger array. The old array will automatically get garbage collected.

  3. #3
    Junior Member kite98765's Avatar
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Grade Array Trouble

    Thank you HelloWorld, but yes, I am limited to standard Java arrays. I asked my profesor about ArrayList but he said he didn't want it.

  4. #4
    Junior Member kite98765's Avatar
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Grade Array Trouble

    Also, could you give an example of a code fragment that might help? Originally, I had the code where it took the total amount of students that were entered, and if it didn't surpass the limits of the array, I subtracted the number of students entered from the limit and told them that was their remaining limit to enter. I just can't get the placement right on it though. Plus, it doesn't stop the user at the limit of 20. It just keeps going into the negatives.
    It looks something like this:
    System.out.println("Would you like to add more student grades?(Y/N)");
    		answer = indata.nextLine();
    		if(answer.equals("Y") || answer.equals("y")){
    			System.out.println("How many more students would you like to add?(1-" +(20 - students)+ ")");
    			add = indata.nextInt();
    		}
    Last edited by kite98765; January 7th, 2010 at 08:20 PM.

Similar Threads

  1. Help with JAVA (Grade Book)
    By Sara_21 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2009, 10:16 PM
  2. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM
  3. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM
  4. Having trouble redirecting nodes
    By KingLane in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2009, 06:46 PM
  5. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM