user13818052
user13818052

Reputation:

Java: Program is not counting occurrences of element inside arraylist

I am making a program that will count the occurrences of "red" and "blue" cars when user inputs a string of the colours of the cars passing by on one line.

I have tried to implement an arraylist and counter but it does not seem to be working. I would just like to to know what am I doing wrong?

Code:

package com.company;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;

public class Main {
  public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);

     String red = "red";
     String blue = "blue";
     int red_count =  0;
     int blue_count =  0;
     System.out.println("Cars: ");
     String x = scanner.nextLine();

     ArrayList<String> arr = new ArrayList<String>();
     arr.add(x);

     for (int i = 0; i < arr.size(); i++) {
        String c = arr.get(i);
        if (c.equals("red")) {
            red_count = red_count + 1;
        }
        if (c.equals("blue")) {
            blue_count = blue_count + 1;
        }
        System.out.println("red: " + red_count);
        System.out.println("blue: " + blue_count);
     }
    }
   }

Input: red green blue yellow

Output:

red: 0

blue: 0

Upvotes: 0

Views: 91

Answers (4)

murari99732
murari99732

Reputation: 159

Have a look it will work as per your requirement

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String red = "red";
        String blue = "blue";
        System.out.println("Cars: ");
        String x = scanner.nextLine();

        String[] brr = x.split(" ");

        Map<String, Long> collect = Arrays.stream(brr)
                .filter(a->a.equals(red)||a.equals(blue))
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
       
        System.out.println(collect);

    }

Upvotes: 1

rifat cakir
rifat cakir

Reputation: 61

You should use HashMap for these kinds of problems to find occurrences.

This solves all in one,

HashMap<String, Integer> cars = new HashMap<String, Integer>();

cars.put(x, (cars.get(x) != null) ? cars.get(x) + 1 : 1);


System.out.println("Red cars: " + cars.get("red"));
System.out.println("Blue cars: " + cars.get("blue"));

For more information check this link.

Upvotes: 0

user10992464
user10992464

Reputation:

You have 2 mistakes in your code:

 String x = scanner.nextLine();

You take 1 line of input, which (according to your post) is "red green blue yellow". When you add this to the list, you don't separate the words, so the list will only contain one element (your full input string), not 4. If you want to separate the words in the input, use x.split("\\s") and add all elements of the array to the list (or just use the array from that point on).

The second mistake is in your output: you have the System.out.println() statements within the loop, so it displays the red and blue car's count every time.

Upvotes: 0

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11126

Change the line:

ArrayList<String> arr = new ArrayList<String>();
arr.add(x);

to

List<String> arr = Arrays.asList(x.split(" "));

You need to split the line by space.

Entire code:

import java.util.Scanner;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String red = "red";
        String blue = "blue";
        int red_count =  0;
        int blue_count =  0;
        System.out.println("Cars: ");
        String x = scanner.nextLine();

        List<String> arr = Arrays.asList(x.split(" "));

        for (int i = 0; i < arr.size(); i++) {
            String c = arr.get(i);
            if (c.equals("red")) {
                red_count = red_count + 1;
            }
            if (c.equals("blue")) {
                blue_count = blue_count + 1;
            }
        }
        System.out.println("red: " + red_count);
        System.out.println("blue: " + blue_count);
    }
}

Upvotes: 0

Related Questions