-4

I wanted to create a method to search through a array list to see if the objects in the array list end with the given char character and then return the count of the character as a int.

public int OccurencesOfCharacter(Char givenCharacter){
....
}

Here's my array List

private ArrayList<Class> list;

For example if the Array List Contained [ Games, iphones] and my given occurrence character was 's' i want to return 2 because it appears twice at the end of the strings.

DrExpresso
  • 73
  • 1
  • 7

3 Answers3

0

Have a look on the sample below.

FULL CODE

import java.util.ArrayList;

public class Sample {

    private static ArrayList<String> list;

    public static void main(String[] args) {
        list = new ArrayList<>();
        list.add("Games");
        list.add("iphones");
        System.out.println(OccurencesOfCharacter('s'));
    }

    public static int OccurencesOfCharacter(char givenCharacter) {
        int count = 0;

        for(String s: list) {
            if(s.charAt(s.length()-1)==givenCharacter)
                count++;
        }

        return count;
    }

}

Other notes:

  • A char in Java is defined through the keyword char, not Char. You also could use the object Character, but never Char, unless it is some class defined by you.
  • I changed the type of your ArrayList from Class to String, since I'm not sure if Class is what you want to use.

Hope it helped!

LuisFerrolho
  • 417
  • 3
  • 13
0
package demppractice;

import java.util.ArrayList;

public class AraylistEndWithS {
    public static void main(String args[])
    {
        ArrayList<String> list=new ArrayList<>();
        list.add("reshma");
        list.add("ramesh");
        list.add("kris");
        list.add("Games");
        list.add("phones");
        countOccouranceOfWord("s",list);
    }

   public static void countOccouranceOfWord(String ch,ArrayList<String> list)
    {
        int count=0;
        for(String ex:list)
        {
            if(ex.toLowerCase().endsWith(ch.toLowerCase()))count++;
        }
        System.out.println("count="+count);
    }
}
Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30
0

Using char.I have use here ASCII for comparing capital and small letter.hope its helpful to you.

package demppractice;

import java.util.ArrayList;

public class AraylistEndWithS {
    public static void main(String args[])
    {
        ArrayList<String> list=new ArrayList<>();
        list.add("reshma");
        list.add("ramesh");
        list.add("kris");
        list.add("Games");
        list.add("phones");
        countOccouranceOfWord('s',list);
    }

    public static void countOccouranceOfWord(char ch,ArrayList<String> list)
    {
        int count=0;
        for(String ex:list)
        {
            char endwithchar=ex.charAt(ex.length()-1);
            int ascii = (int) endwithchar;
            int ascii2 = (int) ch;
            System.out.println("endwithchar="+endwithchar+"===ascii="+ascii);
            if(ascii==ascii2)count++;
        }
        System.out.println("count="+count);
    }
}
Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30
  • Java uses Unicode, not ASCII. As explained by the [Java documentation](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html) `charAt` returns a UTF-16 code unit. UTF-16 is one of several encodings for the Unicode character set. – Tom Blodget Nov 24 '17 at 23:27