4 Replies - 433 Views - Last Post: 27 April 2012 - 09:12 PM Rate Topic: -----

#1 TheYoungWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-April 12

Why is my program not showing results for second array?

Posted 27 April 2012 - 06:48 PM

I need the program to print out the the number of A's,B's,C's etc from the array. The output for my first part is correct but my second array is not showing up when I run the program. Heres my code

package program6;

public class Program6 {

    public static void main(String[] args) {
        int[] testOne = new int[]{60, 99, 75, 81, 78, 99, 71, 99, 67, 78, 67, 61, 84, 65, 85, 51, 52, 97, 53, 61, 76, 96, 95, 88, 80, 87, 77, 67, 79, 97, 63, 98, 98, 68, 82, 82, 68, 83, 83, 50, 56, 92, 79, 69, 64, 74, 63, 50, 56, 73, 78, 74, 71, 54, 73, 59, 64, 58, 60, 83, 81, 88, 70, 80, 96, 50, 98, 65, 60, 65, 58, 99,
            52, 92, 66, 76, 97, 58, 95, 58, 98, 56, 60, 58, 67, 73, 73, 55, 85, 54, 79, 61, 56, 89, 79, 95, 76, 81, 53, 91};

        int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;

        for (int i = 0; i < testOne.length; i++) {

            if ((testOne[i] >= 90) && (testOne[i] <= 100)) {
                gradeA++;
            }
            if ((testOne[i] >= 80) && (testOne[i] < 90)) {
                gradeB++;
            }

            if ((testOne[i] >= 70) && (testOne[i] < 80)) {
                gradeC++;
            }

            if ((testOne[i] >= 60) && (testOne[i] < 70)) {
                gradeD++;
            }

            if (testOne[i] < 60) {
                gradeF++;
            }
        }

        System.out.println(" Exam 1");
        System.out.println("Number of A's: " + gradeA);
        System.out.println("Number of B's: " + gradeB);
        System.out.println("Number of C's: " + gradeC);
        System.out.println("Number of D's: " + gradeD);
        System.out.println("Number of F's: " + gradeF);

    }
    int[] examTwo = new int[]{59, 98, 75, 80, 76, 97, 70, 97, 64, 76, 65, 59, 83, 63, 82, 50, 49, 95, 50, 59, 74, 93, 94, 85, 78, 87, 76, 65, 77, 95, 61, 96, 96, 67, 82, 81, 65, 81, 80, 47, 56, 92, 78, 67, 61, 71, 62, 49, 56, 73, 75, 71, 71, 53, 72, 58, 62, 58, 58, 83, 80, 85, 67, 78, 95, 50, 96, 62, 60, 64, 56, 98, 51, 89, 65, 74, 96, 58, 95, 
        55, 97, 56, 60, 56, 67, 73, 73, 54, 83, 51, 76, 60, 54, 86, 77, 94, 73, 78, 50, 89};	
    {
            int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;

        for (int y = 0; y < examTwo.length; y++) {

            if ((examTwo[y] >= 90) && (examTwo[y] <= 100)) {
                gradeA++;
            }
            if ((examTwo[y] >= 80) && (examTwo[y] < 90)) {
                gradeB++;
            }

            if ((examTwo[y] >= 70) && (examTwo[y] < 80)) {
                gradeC++;
            }

            if ((examTwo[y] >= 60) && (examTwo[y] < 70)) {
                gradeD++;
            }

            if (examTwo[y] < 60) {
                gradeF++;
            }
        

        System.out.println(" Exam 2");
        System.out.println("Number of A's: " + gradeA);
        System.out.println("Number of B's: " + gradeB);
        System.out.println("Number of C's: " + gradeC);
        System.out.println("Number of D's: " + gradeD);
        System.out.println("Number of F's: " + gradeF);
        
}
    }
}



and this the output I'm getting..Its not showing the results for Exam 2 but I think I am doing everything right.

Exam 1
Number of A's: 19
Number of B's: 17
Number of C's: 21
Number of D's: 22
Number of F's: 21



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my program not showing results for second array?

#2 oha055  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 249
  • Joined: 02-February 09

Re: Why is my program not showing results for second array?

Posted 27 April 2012 - 07:01 PM

You are closing the main method and then you write the code for the second exam. Just move the closing bracket of the main method to the end of your class. :)

This post has been edited by oha055: 27 April 2012 - 07:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 TheYoungWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-April 12

Re: Why is my program not showing results for second array?

Posted 27 April 2012 - 07:15 PM

but when I do that the program goes into a continuous loop as such:

Exam 2
Number of A's: 0
Number of B's: 0
Number of C's: 0
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 1
Number of B's: 0
Number of C's: 0
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 1
Number of B's: 0
Number of C's: 1
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 1
Number of B's: 1
Number of C's: 1
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 1
Number of B's: 1
Number of C's: 2
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 2
Number of B's: 1
Number of C's: 2
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 2
Number of B's: 1
Number of C's: 3
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 3
Number of B's: 1
Number of C's: 3
Number of D's: 0
Number of F's: 1
 Exam 2
Number of A's: 3
Number of B's: 1
Number of C's: 3
Number of D's: 1
Number of F's: 1
 Exam 2
Number of A's: 3
Number of B's: 1
Number of C's: 4
Number of D's: 1
Number of F's: 1
 Exam 2
Number of A's: 3
Number of B's: 1
Number of C's: 4
Number of D's: 2
Number of F's: 1
 Exam 2
Number of A's: 3
Number of B's: 1
Number of C's: 4
Number of D's: 2
Number of F's: 2
 Exam 2
Number of A's: 3
Number of B's: 2
Number of C's: 4
Number of D's: 2
Number of F's: 2


etc
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Why is my program not showing results for second array?

Posted 27 April 2012 - 07:45 PM



public class Program6 {

	public static void main(String[] args) {
		int[] testOne = new int[]{60, 99, 75, 81, 78, 99, 71, 99, 67, 78, 67, 61, 84, 65, 85, 51, 52, 97, 53, 61, 76, 96, 95, 88, 80, 87, 77, 67, 79, 97, 63, 98, 98, 68, 82, 82, 68, 83, 83, 50, 56, 92, 79, 69, 64, 74, 63, 50, 56, 73, 78, 74, 71, 54, 73, 59, 64, 58, 60, 83, 81, 88, 70, 80, 96, 50, 98, 65, 60, 65, 58, 99,
				52, 92, 66, 76, 97, 58, 95, 58, 98, 56, 60, 58, 67, 73, 73, 55, 85, 54, 79, 61, 56, 89, 79, 95, 76, 81, 53, 91};

		int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;

		for (int i = 0; i < testOne.length; i++) {

			if ((testOne[i] >= 90) && (testOne[i] <= 100)) {
				gradeA++;
			}
			if ((testOne[i] >= 80) && (testOne[i] < 90)) {
				gradeB++;
			}

			if ((testOne[i] >= 70) && (testOne[i] < 80)) {
				gradeC++;
			}

			if ((testOne[i] >= 60) && (testOne[i] < 70)) {
				gradeD++;
			}

			if (testOne[i] < 60) {
				gradeF++;
			}
		}

		System.out.println(" Exam 1");
		System.out.println("Number of A's: " + gradeA);
		System.out.println("Number of B's: " + gradeB);
		System.out.println("Number of C's: " + gradeC);
		System.out.println("Number of D's: " + gradeD);
		System.out.println("Number of F's: " + gradeF);


		int[] examTwo = new int[]{59, 98, 75, 80, 76, 97, 70, 97, 64, 76, 65, 59, 83, 63, 82, 50, 49, 95, 50, 59, 74, 93, 94, 85, 78, 87, 76, 65, 77, 95, 61, 96, 96, 67, 82, 81, 65, 81, 80, 47, 56, 92, 78, 67, 61, 71, 62, 49, 56, 73, 75, 71, 71, 53, 72, 58, 62, 58, 58, 83, 80, 85, 67, 78, 95, 50, 96, 62, 60, 64, 56, 98, 51, 89, 65, 74, 96, 58, 95, 
				55, 97, 56, 60, 56, 67, 73, 73, 54, 83, 51, 76, 60, 54, 86, 77, 94, 73, 78, 50, 89};	

		gradeA = 0; gradeB = 0; gradeC = 0; gradeD = 0; gradeF = 0;

		for (int y = 0; y < examTwo.length; y++) {

			if ((examTwo[y] >= 90) && (examTwo[y] <= 100)) {
				gradeA++;
			}
			if ((examTwo[y] >= 80) && (examTwo[y] < 90)) {
				gradeB++;
			}

			if ((examTwo[y] >= 70) && (examTwo[y] < 80)) {
				gradeC++;
			}

			if ((examTwo[y] >= 60) && (examTwo[y] < 70)) {
				gradeD++;
			}

			if (examTwo[y] < 60) {
				gradeF++;
			}

		}     // <------ you have that } missing
		System.out.println(" Exam 2");
		System.out.println("Number of A's: " + gradeA);
		System.out.println("Number of B's: " + gradeB);
		System.out.println("Number of C's: " + gradeC);
		System.out.println("Number of D's: " + gradeD);
		System.out.println("Number of F's: " + gradeF);

		// }  // <----- and this one is not at the good place
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Why is my program not showing results for second array?

Posted 27 April 2012 - 09:12 PM

Better to use a separate method to perform your calculations



public class Program6 {

	public static void main(String[] args) {
		int[] testOne = new int[]{60, 99, 75, 81, 78, 99, 71, 99, 67, 78, 67, 61, 84, 65, 85, 51, 52, 97, 53, 61, 76, 96, 95, 88, 80, 87, 77, 67, 79, 97, 63, 98, 98, 68, 82, 82, 68, 83, 83, 50, 56, 92, 79, 69, 64, 74, 63, 50, 56, 73, 78, 74, 71, 54, 73, 59, 64, 58, 60, 83, 81, 88, 70, 80, 96, 50, 98, 65, 60, 65, 58, 99,
				52, 92, 66, 76, 97, 58, 95, 58, 98, 56, 60, 58, 67, 73, 73, 55, 85, 54, 79, 61, 56, 89, 79, 95, 76, 81, 53, 91};

		compute(testOne, "Exam 1");

		int[] examTwo = new int[]{59, 98, 75, 80, 76, 97, 70, 97, 64, 76, 65, 59, 83, 63, 82, 50, 49, 95, 50, 59, 74, 93, 94, 85, 78, 87, 76, 65, 77, 95, 61, 96, 96, 67, 82, 81, 65, 81, 80, 47, 56, 92, 78, 67, 61, 71, 62, 49, 56, 73, 75, 71, 71, 53, 72, 58, 62, 58, 58, 83, 80, 85, 67, 78, 95, 50, 96, 62, 60, 64, 56, 98, 51, 89, 65, 74, 96, 58, 95, 
				55, 97, 56, 60, 56, 67, 73, 73, 54, 83, 51, 76, 60, 54, 86, 77, 94, 73, 78, 50, 89};	

        compute(examTwo, "Exam 2");
	}
	
	private static void compute(int[] exam, String number) {
		int gradeA = 0, gradeB = 0, gradeC = 0, gradeD = 0, gradeF = 0;

		for (int y = 0; y < exam.length; y++) {

			if ((exam[y] >= 90) && (exam[y] <= 100)) {
				gradeA++;
			}
			if ((exam[y] >= 80) && (exam[y] < 90)) {
				gradeB++;
			}

			if ((exam[y] >= 70) && (exam[y] < 80)) {
				gradeC++;
			}

			if ((exam[y] >= 60) && (exam[y] < 70)) {
				gradeD++;
			}

			if (exam[y] < 60) {
				gradeF++;
			}

		}   
		System.out.println(number);
		System.out.println("Number of A's: " + gradeA);
		System.out.println("Number of B's: " + gradeB);
		System.out.println("Number of C's: " + gradeC);
		System.out.println("Number of D's: " + gradeD);
		System.out.println("Number of F's: " + gradeF);
		
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1