[Solved] Find String in ArrayList Containing Phrase

public static void main(String[] args) { List<String> data = new ArrayList<String>(); String yourRequiredString = “dessert”; data.add(“apple fruit”); data.add(“carrot vegetable”); data.add(“cake dessert”); for (int i = 0; i < data.size(); i++) { if (data.get(i).contains(yourRequiredString)) { System.out.println(i); } } } Hope this helps… solved Find String in ArrayList Containing Phrase

[Solved] Java Variable Substitution

This is normally solved with just a String#replaceAll, but since you have a custom, dynamic replacement string, you can to use a Matcher to efficiently and concisely do the string replacement. public static String parse(String command, String… args) { StringBuffer sb = new StringBuffer(); Matcher m = Pattern.compile(“\\$(\\d+)”).matcher(command); while (m.find()) { int num = Integer.parseInt(m.group(1)); … Read more

[Solved] JPA EntityManager find method doesn’t work [closed]

You can get CustomerMapping by cardId using JPQL. A more optimal solution will be to use projections. public String getCustomerMappingCustomerId(String cardId) { getEntityManager(); CustomerMapping result = first(em.createQuery( “select c from CustomerMapping c where c.cardId = :cardId”) .setParameter(“cardId”, cardId).getResultList()); return result == null ? null : result.getCustomerId(); } private static <T> T first(List<T> items) { return … Read more

[Solved] java how compare function works

The Comparable interface is used to compare two objects and their order. As per the Javadocs, the compareTo method should return 0 if the two objects are equal, any negative number if this object is “smaller” than the specified other, and any positive number if this object is “larger” than the specified other. What “smaller” … Read more

[Solved] Java Code removing new line

The lines if(Character.isWhitespace(ch)) { spaces++; } else{ if(spaces>=1) { spaces=0; fos.write(‘ ‘); fos.write(ch);} in your code ensures you condense all whitespace characters into a single space. A newline is considered a whitespace character so you skip those as well. If you don’t want to group the newline with the other whitespace in this case a … Read more

[Solved] How could a for loop be applicated in this?

If you are a begginer, its good that you didnt copied ready solution from the internet : ) You just need to think more about it. Solution is very simple, your program need to know number of the line ‘you’ are currently at how many X to print on that line Your loop should print … Read more

[Solved] Calling methods on a List object of type Class [closed]

Some Background It not exactly clear what your asking… but it sounds like you want to know why you can’t call the methods defined in MessageHandler from your list test of type List<MessageHandler>. The definition of List<E> in the javadoc explains that the parameter E is the type that the list can contain. The add(E … Read more