Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package quera;

import java.util.Scanner;
public class Quera {

  
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How many students do you have?");
        int n = scanner.nextInt();

        for (int i = 0 ; i < n ;i++){
        System.out.println("Enter their names and the number of courses in order :");
        String student1 = scanner.next();
        int course = scanner.nextInt();
        System.out.println("Enter her\\his scores : " );
            for(int x =0 ; x < course ; x++){
                  int score = scanner.nextInt();
               long[][] scores = new  long[n][];
               
              
        }
            
       
         }
        
       
    }
    
}

What I have tried:

I need to save the scores in a 2D jagged array and then print them all with the average grades of each student.
Posted
Updated 22-Oct-21 2:33am

You need to allocate each sub-array as you process each student's data.
Java
long[][] scores = new  long[n][];
score[i] = new long[course];
//
// you now add each course score in order to the score[i] array.
// you probably also need to keep a note of the course count in the array,
// so you do not try and access non-existent elements later.
 
Share this answer
 
Comments
MELIKA Kazemi 22-Oct-21 8:39am    
Thank you so much.It was really helpful.
In the end I need to print all the scores with their average Should I use this array?
How can I pull out scores out of the loop?
Richard MacCutchan 22-Oct-21 8:56am    
Think about what you are trying to do, one step at a time. For each sub-array you need to remember how many scores are inside it. So instead of new long[course]; use new long[course + 1];. You can then put the count of courses in element[0], and the actual scores in the remainder. You can also use that value later on, as a loop counter, when you need to print the scores. So something like:
score[i] = new long[course + 1];
score[i][0] = course; // save the count of courses
for (int j = 1; j <= course; j++) {
    // get the score for the next course
    // save it in the next element
    score[i][j] = // the score for this course
}

You may also need separate array for the student names.
package quera;

import java.util.Scanner;
public class Quera {

  
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How many students do you have?");
        int n = scanner.nextInt();

        for (int i = 0 ; i < n ;i++){
        System.out.println("Enter their names and the number of courses in order :");
        String student1 = scanner.next();
       
        int course = scanner.nextInt();
        
        System.out.println("Enter her\\his scores : " );
        long[][] scores = new  long[n][];
               scores[i] = new long[course];
            for(int x =0 ; x < course ; x++){
             
               
              

              
        }
         
       
         }
        
       
    }
    
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900