-1

I have trouble completing a section of a programme which I got as an assignment in my Java class. I will try to explain my situation as much as I can.

There are 3 arrays in a database.

String[] students = {"A","B","C","D","E"};
int[] mathsMarks = {95,50,-1,80,-1};

I have to print student names and their respective subject marks using print line commands. In doing so, I have to avoid printing instances where there are -1 instances.

Final output should looks like this: Output(Appreciate if somebody can edit the question to embed this image)

My understanding is I have to create 2 new temporary arrays and get rid of -1 instances and their respective student names. But I cannot comprehend the if statements to achieve this. Thanks in advance.

Edit: Edited the question, now there are only 2 arrays.

Klord
  • 5
  • 4

1 Answers1

0

You have to avoid using arrays in this situation. It's better to create a class for the student and collect all data there. Remember, you can treat an array as an object with not changeable length.

class Student {
    private final String name;
    private int mathsMark;
    private int scienceMarks; 
}

Student A = new Student("A", 95, 80);
Student B = new Student("B", 50, 75);
Student C = new Student("C", -1, -1);
Student D = new Student("D", 80, 90);
Student E = new Student("E", -1, -1);

Student[] students = { A, B, C, D, E};

In case you want to collect and then add/remove students, it is better to use a Map:

Map<String, Student> map = new HashMap<>();
map.put(A.getName(), A);
map.put(B.getName(), B);
map.put(C.getName(), C);
map.put(D.getName(), D);
map.put(E.getName(), E);

map.remove(C.getName());
map.remove(E.getName());

In case you still want to use an arrays, this is the solution

public static void main(String... args) throws IOException {
    String[] students = { "A", "B", "C", "D", "E" };
    int[] mathsMarks = { 95, 50, -1, 80, -1 };
    int[] scienceMarks = { 80, 75, -1, 90, -1 };

    assert mathsMarks.length == scienceMarks.length;

    int length = students.length;

    for (int i = 0; i < mathsMarks.length; i++)
        if (mathsMarks[i] < 0 || scienceMarks[i] < 0)
            length--;

    String[] newStudents = new String[length];
    int[] newMathsMarks = new int[length];
    int[] newScienceMarks = new int[length];

    for (int i = 0, j = 0; i < mathsMarks.length; i++) {
        if (mathsMarks[i] >= 0 && scienceMarks[i] >= 0) {
            newStudents[j] = students[i];
            newMathsMarks[j] = mathsMarks[i];
            newScienceMarks[j] = scienceMarks[i];
            j++;
        }
    }

    System.out.println(Arrays.toString(newStudents));
    System.out.println(Arrays.toString(newMathsMarks));
    System.out.println(Arrays.toString(newScienceMarks));
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Im still a beginner to coding. Dont understand what you meant here. Either way, this is how the question was presented. I have to search for -1 instances and delete them alongside the respective element in students array. I also edited the question, there are only 2 arrays. – Klord Oct 22 '22 at 19:55
  • @Klord I have attached solution with an arrays – Oleg Cherednik Oct 22 '22 at 20:04
  • Thank you, this helped. Didnt use the code exact way, but I was able to derive a code from your answer. However, in this specific code, whats the purpose of j++ in the the final for loop? – Klord Oct 22 '22 at 22:43
  • `j++` is the iterator of the new array – Oleg Cherednik Oct 22 '22 at 22:52