What error message did the compiler give you for the error? There could be an explanation there.Quote:
tell me whats wrong in this
Printable View
What error message did the compiler give you for the error? There could be an explanation there.Quote:
tell me whats wrong in this
Thanks Faz I got it with no errors! One thing though, how can I make a single array element equal to more than one number this wont work
and not sure what will...Code :grd[0] = 60,75,93; //or grd[0] = {60,75,93};
(thanks for pointing out the "it" Norm :D)
What is the it in the above?Quote:
how can I make it equal to more than one number
A variable or an element of an array can only hold one value at a time. You can change the values one after the other so that it has held all the values. But at any instance of time, it will only hold one value.
To initialize an array of ints you can code: int[] ints = {1,2,3}; // create an array with 3 elements and put values into each
The code I posted initialises a 2D array with contents, creating an array of two arrays of 3 ints. The outer curly braces define the bounds of the outer array. Inside these, each pair of inner curly braces defines an element of the outer array. Inside the inner pairs of curly braces, the elements of the inner arrays are defined.
I don't know what your needs are, so I can't tell you how to modify it to suit them. Once you understand the code, you should be able to do it yourself.
Hiya guys, Thanks for your help! But I can't seem to figure it out, i'm gonna head to the books site and see the code later(if I don't get posts telling me to "wait for a sec!!" :/).
The assignment statement in the for loop is assigning a value to an element (at i-1) of the array my2dArray.Quote:
my2dArray[i-1] = new int[i];
The value being assigned is another array of dimension i.
I tried a few more times but I just couldn't figure it out, I finally directed my browser to the site today and here is the code
run:Code :class StudentGrades { public static void main(String arguments[]) { int[][] student = new int[6][6]; int[] janice = { 90,85,85,75,70,95 }; int[] mikey = { 95,85,85,70,75,90 }; int[] nicki = { 85,80,85,95,100,100 }; int[] bernice = { 80,90,90,80,85,95 }; int[] ernest = { 75,45,80,95,90,90 }; int[] bigMike = { 90,90,90,95,70,65 }; student[0] = janice; student[1] = mikey; student[2] = nicki; student[3] = bernice; student[4] = ernest; student[5] = bigMike; int gradeSum = 0; int[] studentSum = new int[6]; // Loop through the students for (int i = 0; i < 6; i++) { System.out.println("Student #" + i); studentSum[i] = 0; // Loop through each student's grades for (int j = 0; j < 6; j++) { gradeSum = gradeSum + student[i][j]; studentSum[i] = studentSum[i] + student[i][j]; } System.out.println(" Average grade: " + studentSum[i] / 6); } System.out.println("\nAverage grade of all students: " + gradeSum / 36); } }
Student #0
Average grade: 83
Student #1
Average grade: 83
Student #2
Average grade: 90
Student #3
Average grade: 86
Student #4
Average grade: 79
Student #5
Average grade: 83
Average grade of all students: 84
and here is the code I wrote
run:Code :class NewAry { public static void main(String[] args){ int[] stud = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int[] alem = {60,70,85}; int[] jon = {99,23,58}; int[] yohanes = {86,33,67}; int[] hagos = {35,26,98}; int[] grade = new int [3]; grade[2] = hagos[2]; System.out.println(grade[2]); } }
98
One other problem I had is I didn't know you can do this
Code :int[][] student = new int[6][6]; student[0] = janice;
with a two dim array. But when I tried it
got this output : [I@3e25a5Code :int[][] test = new int [4][4]; test[3] = hagos;
I'm not sure why I got this out put [I@3e25a5 while it worked fine in the code from the site...
And thats what I need help with right now why I got that funny output?8-x
Thanks every one for your help!!! Here's a Thank you! cup of coffee to every one!~o)
I don't see where your println() is that generated the output you show.
[I@3e25a5 is what is generated by the toString() method for the array object. Arrays are objects. The Object class has a toString() method that returns a String with data about the object.
The [ indicates an array, the I indicates integer, the @3e25a5 is the address in memory.
I'm having a little trouble understanding the for loop in the StudentGrader class, can you make it a little clearer please?
Code :class StudentGrades { public static void main(String arguments[]) { int[][] student = new int[6][6]; int[] janice = { 90,85,85,75,70,95 }; int[] mikey = { 95,85,85,70,75,90 }; int[] nicki = { 85,80,85,95,100,100 }; int[] bernice = { 80,90,90,80,85,95 }; int[] ernest = { 75,45,80,95,90,90 }; int[] bigMike = { 90,90,90,95,70,65 }; student[0] = janice; student[1] = mikey; student[2] = nicki; student[3] = bernice; student[4] = ernest; student[5] = bigMike; int gradeSum = 0; int[] studentSum = new int[6]; // Loop through the students for (int i = 0; i < 6; i++) { System.out.println("Student #" + i); studentSum[i] = 0; // Loop through each student's grades for (int j = 0; j < 6; j++) { gradeSum = gradeSum + student[i][j]; studentSum[i] = studentSum[i] + student[i][j]; } System.out.println(" Average grade: " + studentSum[i] / 6); } System.out.println("\nAverage grade of all students: " + gradeSum / 36); } }
The 2 dim array holds student data. The i index is for the student and the j index is for the scores for that student.
studentSum[i] = studentSum[i] + student[i][j];
or
studentSum[i] += student[i][j];
Its incrementing the sum for student i by all of his scores indexed by j
Thanks guys for all your help!! :D I'm marking this Solved!
Thanks again!