1

I have two char arrays. One is the correct answers to the exam, char[] answers and the other is the char[] studentAnswers. I want to compare the two arrays and if there is an incorrect answer I want to store what questions they got wrong.

questionsMissed = i
questionsMissed = studentAnswers[i]

public int[] questionsMissed(char[] studentAnswers)
{
  for(int i = 0; i<studentAnswers.length; i++){
    if(studentAnswers[i] != answers[i]){
      questionsMissed =;
    }
  }
  return questionsMissed;
}

I want questionMissed to store the index of the char values that do not match.

1
  • Then simply allocate a new array of the same size as the number of questions. All you need to to do is store a 1 in the proper slot for that value of i if they got the question wrong or a 0 if they got it correct. Commented Oct 16, 2019 at 16:17

2 Answers 2

1

Probably you need a List of Integer to add indexes and then convert it into int array

public int[] questionsMissed(char[] studentAnswers) {

List<Integer> list = new ArrayList<>();

for(int i = 0; i<studentAnswers.length; i++){
   if(studentAnswers[i] != answers[i]){
      list.add(i);
     }
   }
 return list.stream().mapToInt(Integer::intValue).toArray();
}

You can do this easily by using java-8 stream

int[] res = IntStream.range(0, studentAnswers.length)
                     .filter(i->studentAnswers[i] != answers[i])
                     .toArray();

If you you are on java-7 you need to iterate the list again

public int[] questionsMissed(char[] studentAnswers) {

List<Integer> list = new ArrayList<>();

for(int i = 0; i<studentAnswers.length; i++){
   if(studentAnswers[i] != answers[i]){
      list.add(i);
     }
   }
    int[] arr = new int[list.size()];

    for(int i=0; i<arr.length; i++) {
        arr[i]=list.get(i);
    }
  return arr;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have not learned java-8 stream yet as in my classes we have not gotten that far. Can you provide any answer without using this.
When I ran that it referenced the memory where it was stored and not the index of the incorrect question. This is what the textbook ask for. questionsMissed. An int array containing the question numbers of the questions that the student missed
Sorry i did not get your question @Ross
1

It is easier if you use a list to append the indexes and return an int array at the end:

public int[] questionsMissed(char[] studentAnswers)
{
  List<Integer> questionsMissed  = new ArrayList<>();
  for(int i = 0; i<studentAnswers.length; i++){
    if(studentAnswers[i] != answers[i]){
      questionsMissed.add(i);
    }
  }
  return questionsMissed.toArray();
}

1 Comment

List.toArray() returns object but not int array

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.