Does this class comply to Factory method pattern?

public class ServiceFactory { private static ServiceFactory instance; private final DishService dishService = new DishService(); private final OrderService orderService = new OrderService(); private final UserService userService = new UserService(); private ServiceFactory() { } public static synchronized ServiceFactory getInstance() { if (instance == null) instance = new ServiceFactory(); return instance; } public DishService getDishService() { return… Read More Does this class comply to Factory method pattern?

Type mismatch: cannot convert from Object Array to int

import java.util.*; public class tryAgain { public static void main(String[] args) { Scanner sc = new Scanner (System.in); //Creating array with Children object Children[] ages = new Children[5]; //Creating new Children ages and adding em to counter ages[0] = new Children(12); ages[1] = new Children(10); ages[2] = new Children (13); ages[3] = new Children(9); ages[4]… Read More Type mismatch: cannot convert from Object Array to int

Appending to file in static methods keep rewriting the whole file in Java

I have a static method in which I want to write something to a file, but it keeps rewriting the whole file. public static void log(Class called, String functionName, String problem) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("log.txt")); bw.append(LocalDateTime. now() + " – " + called.getCanonicalName() + " – " + functionName +… Read More Appending to file in static methods keep rewriting the whole file in Java

JAVA : How can I reverse the string and parse it with '-' in Java

I have to reverse the given string and parse it with "-". for eg. INPUT: abcde OUTPUT : e-d-c-b-a Any simple solutions? >Solution : Using StringBuilder#reverse we can try: String input = "abcde"; StringBuilder sb = new StringBuilder(input); String output = sb.reverse().toString().replaceAll("(?<=.)(?=.)", "-"); System.out.println(output); // e-d-c-b-a For an explanation on the regex pattern used in… Read More JAVA : How can I reverse the string and parse it with '-' in Java