-
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:
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();
}
}
}
-
Re: Help with my array
- Replace in.nextLine() with in.next().
- Make sure the Student constructor assigns the parameter values to the fields.
-
Re: Help with my array
So, I changed the parameters. but I'm still getting null in my arrays:
Code:
class Student {
public Student(int studentnumber, String surname, String firstname,
String country) {
}
int studentnumber;
String surname;
String firstname;
String country;
}
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:
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);
}
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 on
Here I get an error: java.lang.ArrayIndexOutOfBoundsException: 1
at ArrayOfObjects.main(employee.java:124)
what does that mean?
-
Re: Help with my array
Assignment from parameter to field does not happen automagically. It should look like this:
Code:
class SomeDemoClassToProveAPoint {
int value;
String name;
public SomeDemoClassToProveAPoint(int valueParameter, String nameParameter) {
this.value = valueParameter;
this.name = nameParameter;
}
}
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.
-
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?
-
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:
Code:
Student[] students = new Student[numofstudents];
Grade[] grades = new Grades[numofstudents];
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:
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;
This example above is based on your Grade class.
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.
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;
}
}
All you need to do now is:
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() );
I am not sure if this is what you want. Again, this is based on assumptions.