0

i have a little task in java generated from an exercise randomizer application. The instructions are:

Write a class named Vulgarist with a public identifier which must contain a public static method mes. This method gets as arguments a Vector type collection of Long and two tables with boolean type elements. The two tables and the collection have the same number of elements. The method returns as a result a Vector type collection of Long consisting of the elements of the collection of the first argument for which the corresponding element of the first table is the value true and the corresponding element of the second table is the value false.

My code is:

import java.util.Vector;
public class Vulgarist {
    public static Vector<Long> mes(Vector<Long> v1, boolean a[], boolean b[]) {
        Vector<Long> v2 = new Vector<>();
        while ((v1.size() == b.length) && (v1.size() == a.length)
            && (a.length == b.length)) {
            for (int i = 0; i <= a.length; i++) {
                if ((a[i] == true) && (b[i] == false)) {
                    v2.add(v1.get(i));
                }
            }
        }
        return v2;
    }
}

The result i get from the application is "The method mes was not implemented according to the instructions" Message Error : null

Can anyone help me find what i did wrong?

1 Answer 1

1

There are 2 main problems:

  1. Your while loop is unnecessary, and will always evaluate to true, thus resulting in an endless loop. Just remove the while loop. It doesn't accomplish anything.
  2. The for condition i <= a.length should be changed to i < a.length to avoid getting an IndexOutOfBoundsException. Remember that the last available index on an array is length - 1.
Sign up to request clarification or add additional context in comments.

1 Comment

Wow at first i had no while loop and added it later cause i thought that it was the reason of my problem. Turns out, all this time the problem was in the for as u correctly suggested. Such a small thing. Thank you.

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.