-1

I am currently stuck on this program where the objective is to take input of the size of the array and then to each row of the array with string input of colors.Each row can fit in as many colors. By using another method to check If the row consists of atleast 50% or more of the color blue it will return a "Pass" value or else a "Failure" value. Example input:

2 blue red green blue blue orange red

Example Output:

Failed Pass

My problem is in the check method at line 39 of my code I end up getting a NullPointerException with this line if(x[i].contains(blue))

Is there a efficient way to count how many times "blue" appears and adding it to a counter?

Here is the code: ```

public class BigBlue {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        String[] list = new String[x];
        String inputs = input.next();
        for(int i = 0;i < list.length;i++) {
            for(int j = i; j < i; j++) {
            if(inputs == "blue") {
                list[i] = inputs;
            }
            else if(inputs == "orange") {
                list[i] = inputs;
            }
            else if(inputs =="red") {
                list[i] = inputs;
            }
            else if(inputs == "green") {
                list[i] = inputs;
            }
            else {
                System.out.println("Invalid input");
            }
            }
        }
        System.out.println(check(list));

    }

    public static String[] check(String[] x) {
        String[] j = new String[x.length];

        for(int i = 0; i < x.length;i++) {
            int wordCount = 0;
            int counter = 0;
            if(x[i].contains("blue")) {

                counter++;
            }
            String trim = x[i].trim();
            wordCount = trim.split("\\s+").length;
            int k = wordCount / 2;
            if(counter >= k) {
                j[i] = "Passed";
            }
            else {
                j[i] = "Failed";            }
        }
        return j;
    }

}

Janes
  • 29
  • 6

2 Answers2

0

Use ArrayList instead of simple arrays, because the size() operator of an ArrayList returns the real number of elements instead of the possible maximum number of elements.

You may use HashMap to count the number of elements:

String[] inputArray = {"a", "a", "b", "b", "b", "b", "a", "a", "a", "b", "c", "c", "c", "e", "c", "d", "d", "c", "c", "c", "c", "d"};

Map<String, Integer> map = new HashMap<String, Integer>();

for (String element : inputArray) {
    Integer count = map.get(element);

    // Increase the counter
    if (count == null) {
         count = 1;
    } else {
         count++;
    }

    map.put(element, count);
}

Finally the map contains the count of each different string.

With only 10 elements, your nested loops may be quicker, but as more elements you have as quicker the HashMap will become (relative to nested loops).

Stefan
  • 1,645
  • 1
  • 10
  • 15
-1

You could try using a variable like count and then just use count++?