Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
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 student = scanner.next();
        int course = scanner.nextInt();
        System.out.println("Enter her\\his scores : " );
        
        int[][] scores = new  int[n][];
             
            for(int x =0 ; x < course ; x++){
                 scores[n][course] =  scanner.nextInt();     
            }
        }
    }
}


What I have tried:

I m trying to store all scores in a 2D jagged array (I need to use those scores out of the loop to calculate the average of every student and find the highest one.)
Posted
Updated 22-Oct-21 22:43pm
v3
Comments
MELIKA Kazemi 22-Oct-21 15:14pm    
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at quera.Quera.main(Quera.java:23)
Patrice T 22-Oct-21 16:16pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

This is a repeat fo your previous question at How can I store students scores outside the loop in a 2D jagged array? ([^]. Go back there and read my suggestions again. You must allocate the main array before you start the loop, based on the number of students:
Java
System.out.println("How many students do you have?");
int n = scanner.nextInt();
int[][] scores = new int[n][];

Then when you start each new student's entries inside the main loop, you need to allocate the student array as I suggested in the other question.
 
Share this answer
 
Comments
MELIKA Kazemi 23-Oct-21 20:39pm    
I'm working on it and trying for days but I can't solve it.
Could you plz do me a favor and put those arrays inside the code?I tried everything but I still get exceptions
Richard MacCutchan 24-Oct-21 3:34am    
You need to stop coding and think carefully about the steps you need to take to get this working.
1. As I mention above capture the student count.
2. Now start a loop that goes from 0 to n (the number of students).
3. Ask for the student name and number of courses.
4. Create a new array at scores[i] of to hold the marks for each course. The length of the array should be the number of courses.
5. Now start a new (inner) loop to capture the marks.
6. Get the marks for the next course and store it in scores[i][j]
At then end of the inner loop it will start the next iteration to capture the next student's details.
When all students have been processed you should have a jagged array containing all the student marks.

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