-1

I have created a method for my program, where I compare two arrays, user and test. I am trying to add the index of the array user into the ArrayList qMissed when it is not the same as the test Array. If both arrays are the exact same then it should just return null. I am getting exception errors because I need to complete the reference type but I am unsure of what to do.

 /**
 * @param user
 * @param test
 * @return
 */
 public static ArrayList<String> questionMissed(String[] user, String[] test) 
 {   
    ArrayList<int> qMissed = new ArrayList<int>();
    for (int i = 0; i <= user.length-1; i++)
    {
       if (user[i] != test[i])
       {
          qMissed = Arrays.asList(qMissed).indexOf(user);(i+1);
       }
       else if (user[i] == test[i])
       {
          return null; 
       }
    }
    return qMissed;
 }
Mike
  • 4,722
  • 1
  • 27
  • 40
pchova
  • 99
  • 6
  • Cannot use a primitive as a generic type parameter. Use `Integer`. – Andreas Oct 17 '18 at 16:51
  • `i` is the index you want to add to the `qMissed` list, so add it: `qMissed.add(i)` – Andreas Oct 17 '18 at 16:51
  • Don't `return null` inside the loop, since you don't know yet whether anything missed. – Andreas Oct 17 '18 at 16:52
  • See: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Oct 17 '18 at 16:52
  • 1
    Use an IDE so you'll see syntax errors immediately, and fix them before there are too many to decipher. – Andreas Oct 17 '18 at 16:54
  • 1
    There are many problems in your program: The generic type has to be an object type, not a primitive; you return null as soon as one element is equal, instead of returning it at the end; you do something very strange with `qMissed` instead of just adding the index to it. And you compare strings with `==`. – RealSkeptic Oct 17 '18 at 16:56

4 Answers4

1

Your method seems to have some logical and compilation issues.

Looks like you need this method,

public static List<Integer> questionMissed(String[] user, String[] test) {
    List<Integer> qMissed = new ArrayList<Integer>();

    for (int i = 0; i < user.length; i++) {
        if (!user[i].equals(test[i])) {
            qMissed.add(i);
        }
    }

    return qMissed.size() == 0 ? null : qMissed;
}

Fixes and their explanation,

1. Your return type has to be List<Integer> instead of ArrayList<String> because you want to return an ArrayList of Integer indexes and not string.
2. Second problem you can't use primitive type in ArrayList<int> instead you need to use ArrayList<Integer>
3. You can't compare strings with == instead you need to use equals method on string.
4. You don't have to return null inside forloop else hence else block I have removed.
5. After you exit the forloop, as you want to return null if both element's arrays matched hence this code,

return qMissed.size() == 0 ? null : qMissed;

Let me know if you face any issues using this method.

Edit:

How to display "All are correct" message in case both passing arrays have same numbers. You must be calling it something like this,

List<Integer> list = questionMissed(user,test);
if (list == null) {
    System.out.println("All are correct");
} else {
    // your current code
}
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • It would be better to explain what the logical problems are instead of just giving unexplained different code. – RealSkeptic Oct 17 '18 at 16:57
  • Agree. Although mostly others have already told the issues but lemme update in my answer too. – Pushpesh Kumar Rajwanshi Oct 17 '18 at 16:59
  • "if (!user[i].equals(test[i])) " this line could throw exception ArrayIndexOutofBound for test[i]. test array could be smaller here. need a check – Pandey Amit Oct 17 '18 at 17:09
  • Well, the OP hasn't mentioned about both arrays being not equal. In that case I think OP will have to tell what behavior she wants. May be in the very beginning just check if both arrays are not equal then throw an exception or may be something else. Unless OP says so, there can be multiple behaviors possible so I would rather assume both arrays are equal. Else OP has to tell what she wants in case both arrays are not equal. – Pushpesh Kumar Rajwanshi Oct 17 '18 at 17:13
  • thank you for explaining the code - but I do have a question : if I wanted to output a string saying something like "all were correct" instead of returning null, what could I do? Because null : qMissed refers to an Array but it was converted to an ArrayList. How can I changed the return statement – pchova Oct 17 '18 at 18:54
  • For displaying "all were correct" instead of null, you can do it from the code where you are calling questionMissed method. Check if returned list is null then display this message. Let me update the answer to include that. – Pushpesh Kumar Rajwanshi Oct 18 '18 at 06:30
0

I see multiple issues with your code, Firstly as Andreas said ArrayList cannot host primitive types so change it to

ArrayList<Integer> qMissed = new ArrayList<Integer>();

Second problem I see is that you are comparing Strings using == this compare can be wrong so use equals instead

(user[i].equals(test[i]))

and the last mistake I see is the code cannot be compiled, can you give me more information in the comments to what you are trying to do in this part since It's not valid code

qMissed = Arrays.asList(qMissed).indexOf(user);
(i + 1);

if you want to do something like Pushpesh Kumar Rajwanshi answer you can use java 8 streams what this does is makes an IntStream with the length of user, and then filters it to have only items if they don't equal at the same index and then adds it to qMissed.

public static List<Integer> questionMissed(String[] user, String[] test) {
    List<Integer> qMissed = new ArrayList<>();
     IntStream.range(0, user.length)
            .filter(i -> !user[i].equals(test[i]))
            .forEach(qMissed::add);
    return qMissed.size() == 0 ? null : qMissed;
}
SamHoque
  • 2,978
  • 2
  • 13
  • 43
0

You can try changing the return type from ArrayList to ArrayList in the method:

public static ArrayList<int> questionMissed(String[] user, String[] test) {   
        ArrayList<int> qMissed = new ArrayList<int>();
        for (int i=0;i<=user.length-1;i++) {
            if (user[i] != test[i]) {
                qMissed = Arrays.asList(qMissed).indexOf(user);(i+1);
            } else {
                return null; 
            }
       }
       return qMissed;
    }

Also you can remove the else if condition cause is redundant. Please attach the exceptionsyou are getting.

0

I guess you want like below ..

 public static ArrayList<Integer> questionMissed(String[] user, String[] 
        test) {   
    ArrayList<Integer> qMissed = new ArrayList<Integer>();
    int i = 0;
    if(user.length == test.length ) {
        while(i < user.length ){
            if (!(user[i].equals(test[i]))) {
                qMissed.add( i + 1);
            }else {
                return null;
            }
            i++;
        }
        if(user.length > 0 && i == user.length ) {
            return qMissed;
        }
    }
    return null;

    }
Pandey Amit
  • 657
  • 6
  • 19