Results 1 to 6 of 6
Thread: Help with my array
- 11-26-2012, 10:26 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Help with my array
Help with my code. I have some glitches on output. Like in line 46 my input studentnumber gets skipped. My output is null and my average doesn't work only adds them up:
Heres the code:
Java Code:import java.io.*; import java.util.Scanner; class Student { public Student(int studentnumber2, String surname2, String firstname2, String country2) { } int studentnumber; String surname; String firstname; String country; } class Grade{ double grade, average,sum=0; } class ArrayOfObjects { public static void main(String[] args) { double sum=0; try { Scanner input = new Scanner(System.in); //gets number of students System.out.print( "Please enter number of students: " ); int numofstudents = input.nextInt(); //gets number of grades System.out.println("Please enter number of grades: "); int numofgrades = input.nextInt(); /* initialise array with given length */ double[] Grade = new double[numofgrades]; //for grades Student[] myStudent = new Student[numofstudents];//for student info for( int i=0; i < myStudent.length; i++ ) { //gets student number System.out.print( "Student Number " +(i+1)+ ": " ); int studentnumber = input.nextInt(); //gets surname System.out.print( "Surname of Student " +(i+1)+ ": " ); String surname = input.nextLine(); //gets firstname System.out.print( "Firstname of Student " +(i+1)+ ": " ); String firstname = input.nextLine(); //gets country of residence System.out.print( "Country of Residence " +(i+1)+ ": " ); String country = input.nextLine(); //does a for loop based on number of grades for( int j=0; j < Grade.length; j++ ){ //gets grades System.out.print( "Grades " +(j+1)+ ": " ); Grade[j] = input.nextDouble(); //adds up all input into sum sum += Grade[j]; } //stores all student info into myStudent array myStudent[i] = new Student(studentnumber, surname, firstname, country); } //computes average double average = (sum/numofgrades); //loop to print all arrays for (int i=0; i < myStudent.length; i++) { //prints student info System.out.println (myStudent[i].studentnumber + ", " + myStudent[i].surname + ", " + myStudent[i].firstname + ", " + myStudent[i].country); //prints grades System.out.println(Grade); System.out.println(average); } } catch( Exception e ) { e.printStackTrace(); } } }
- 11-26-2012, 11:00 AM #2
Godlike
- Join Date
- Nov 2012
- Posts
- 197
- Rep Power
- 1
Re: Help with my array
- Replace in.nextLine() with in.next().
- Make sure the Student constructor assigns the parameter values to the fields.
- 11-27-2012, 01:16 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Help with my array
So, I changed the parameters. but I'm still getting null in my arrays:
I tried changing average and limited input max 3 students, and max 4 grades with if functions. It looks like even if i print out arrays like this:Java Code:class Student { public Student(int studentnumber, String surname, String firstname, String country) { } int studentnumber; String surname; String firstname; String country; }
i get error. If i recall, when storing values in an array through for loop u get the succession of array[0] then array[1].. so onJava Code:if ((numofstudents==2)&&(numofgrades==2)){ //example number of students is 2 and number of grades is 2 System.out.println("Average of 1st Student: " + (Grade[0]+Grade[1])/numofstudents); System.out.println("Average of 2nd Student: " + (Grade[2]+Grade[3])/numofstudents); }
Here I get an error: java.lang.ArrayIndexOutOfBoundsException: 1
at ArrayOfObjects.main(employee.java:124)
what does that mean?
- 11-27-2012, 01:27 PM #4
Godlike
- Join Date
- Nov 2012
- Posts
- 197
- Rep Power
- 1
Re: Help with my array
Assignment from parameter to field does not happen automagically. It should look like this:
Also, you have a class named Grade, and you introduce a double[] named Grade. Then you refer to it's length by Grade.length. This might be compilable and all, but it's very confusing. Java naming convention says classes start with an uppercase letter and variables with lowercase.Java Code:class SomeDemoClassToProveAPoint { int value; String name; public SomeDemoClassToProveAPoint(int valueParameter, String nameParameter) { this.value = valueParameter; this.name = nameParameter; } }
- 11-29-2012, 11:59 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 9
- Rep Power
- 0
Re: Help with my array
thanks Surfman that really fixed my Array!
my problem now is getting the average if get more users like example 2 , it behaves to add up all grades and devides it by numofgrades. Is there anything you can advice me?
- 11-30-2012, 09:54 AM #6
Godlike
- Join Date
- Nov 2012
- Posts
- 197
- Rep Power
- 1
Re: Help with my array
This is hard to fix without making assumptions, but I'll give it a shot. I assume that every student has a grade. So you should create an array of Student *and* create an array of Grade. The index of each array corresponds to the other array, so Students[0] has Grade[0]. That would look like this:
Now when you have the number of grades for each student, you can start collecting those in a loop, and push those values into a Grade object:Java Code:Student[] students = new Student[numofstudents]; Grade[] grades = new Grades[numofstudents];
This example above is based on your Grade class.Java Code:Grade gr = new Grade(); grades[i] = gr; for ( int y=0; y < numofgrades; y++ ) { double grade = input.nextDouble(); gr.sum += grade; } gr.average = gr.sum / numofgrades;
You could move the responsibility of averaging to the Grade class. In my example, tThe Grade class accepts new grades with the addGrade(double) method. Internally, it is summed and the number of grade is incremented with 1. When you need the average, you can divide it like you did in your example. You can create a class like the one below.
All you need to do now is:Java Code:class Grade { private double sum; private double numberOfGrades; public void addGrade(double grade) { this.sum += grade; this.numberOfGrades++; } public double getAverage() { return sum / numOfGrades; } }
I am not sure if this is what you want. Again, this is based on assumptions.Java Code:Grade gr = new Grade(); grades[i] = gr; for ( int y=0; y < numofgrades; y++ ) { double grade = input.nextDouble(); gr.addGrade(grade); } System.out.println("The average = " + gr.getAverage() );
Similar Threads
-
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks