0

I need to create a 2d array which can read in the student ID's of 50 students and each of their 7 subject marks. I have come up with a way to store subject marks but not sure how to store the student ID's. Here is the code so far.

public static void main(String[] args) 
{
    double mark;
    double[][] studs = new double[50][7];

    Scanner fromKeyboard = new Scanner(System.in);

    for (int studentNo = 0; studentNo < 3; studentNo++) {
        System.out.println("enter student ID number for student " + studentNo);

        for (int moduleNo = 0; moduleNo < 2; moduleNo++) {
            System.out.println("Enter users mark for module " + moduleNo);
            mark = fromKeyboard.nextDouble();
            studs[studentNo][moduleNo] = mark;
        }
    }
}
6
  • 1
    can't you use Collections? Commented Apr 7, 2016 at 14:45
  • 1
    @Jordi Castilla Maybe he doesn't know how? Commented Apr 7, 2016 at 14:48
  • 1
    Or perhaps use objects? What is your goal with the Ids and marks? Commented Apr 7, 2016 at 14:49
  • 1
    @NikolasCharalambidis dunno, that's why am I asking, because seems a homework and homework = limitedResources Commented Apr 7, 2016 at 14:53
  • Best way is to create a Class 'Student' having 'id' and 'marks[]' as its members. And then you can do anything with those 50 Student objects. Commented Apr 7, 2016 at 14:54

4 Answers 4

1

You have only one array of a single primitive type, but you have two pieces of information.

Two simple options are

1) Use another array to store the IDs

2) (Better solution IMO) Create your own Student class, and define an array Student[] (A student should contain a field for an array of marks)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use array[n][0] to store student id.

This should work:

public static void main(String[] args) 
{
    double mark = 0d;
    int id = 0;
    double[][] studs = new double[50][8];

    Scanner fromKeyboard = new Scanner(System.in);

    for (int studentNo = 0; studentNo < 50; studentNo++) {
        System.out.print("enter student ID number for student " + (studentNo + 1) + ":");
        id = fromKeyboard.nextInt();
        studs[studentNo][0] = id;
        for (int moduleNo = 1; moduleNo < 8; moduleNo++) {
            System.out.print("Enter mark of student " + id + " for module " + moduleNo);
            mark = fromKeyboard.nextDouble();
            studs[studentNo][moduleNo] = mark;
        }
    }

    fromKeyboard.close();
}

NOTES:

  • If you cannot modify the original array or you need to store students name, for example, you can create a new array to store students id like.

    String[] studentsId = new String[50];
    int[] studentsId = new int[50];
    
  • remember to close resources when using it: fromKeyboard.close();

Comments

0

When I understood your question correct:

public static void main(String[] args) 
{
    double[][] studs = new double[50][8];

    Scanner fromKeyboard = new Scanner(System.in);

for (int studentNo = 0; studentNo < 50; studentNo++) {
    System.out.println("enter student ID number for student " + studentNo);
    studs[studentNo][0] = fromKeyboard.nextDouble(); //save id
    for (int moduleNo = 1; moduleNo < 8; moduleNo++) {  
        System.out.println("Enter users mark for module " + moduleNo);
        studs[studentNo][moduleNo] = fromKeyboard.nextDouble(); // your 7 marks
    }
}
}

Comments

0

You're on the right track.

You aren't reading the studentNo input. So you need to read that and place it in the first cell before the inner loop. Then put all the marks on the same row along side it. This is depending on the type of student ID, is it a String or number?

Also, why have 7 columns in the array and only loop twice for subject grades? Is there more to do here. If not avoid using up the space.

1 Comment

I was only looping through twice to make it easier to see what was going on and not having to go through 50 students and 7 subjects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.