Results 1 to 6 of 6
  1. #1
    luckycharm is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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();
        }
        
      }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    895
    Rep Power
    2

    Default Re: Help with my array

    - Replace in.nextLine() with in.next().
    - Make sure the Student constructor assigns the parameter values to the fields.

  3. #3
    luckycharm is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with my array

    So, I changed the parameters. but I'm still getting null in my arrays:
    Java 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:
    Java 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?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    895
    Rep Power
    2

    Default Re: Help with my array

    Assignment from parameter to field does not happen automagically. It should look like this:
    Java 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.

  5. #5
    luckycharm is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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?

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    895
    Rep Power
    2

    Default 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:
    Java 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:
    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;
    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.
    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;
       }
    }
    All you need to do now is:
    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() );
    I am not sure if this is what you want. Again, this is based on assumptions.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •