[Solved] Create a Array from HashMap in Java

Try this HashMap<String, String> param = new HashMap<String, String>(); param.put(“A”, “2”); param.put(“B”, “3”); param.put(“C”, “2”); String[] list = new String[7];// It is better to use List other than an array int i = 0; for (Map.Entry<String, String> entry : param.entrySet()) { int lim=Integer.parseInt(entry.getValue()); for(int j=0;j<lim;j++){ list[i]=entry.getKey()+” “+String.valueOf(j+1); i++; } } If you really want to … Read more

[Solved] How to limit decimals digits?

Just use String.format: System.out.println(String.format(“%.2f”,ans)); Note that this converts your value into a String, for calculations you should keep using its numeric representation. solved How to limit decimals digits?

[Solved] Custom Class Loader In Java [closed]

You can use some obfuscation tools, like ProGuard. A self written ClassLoader, must be placed in a standard .class file, that the JVM can load it. And then you secure loader can be reverse engineered. Don’t do it by yourself. Writing “secure” code without knowing cryptographic algorithms will lead to error-prone an insecure code 5 … Read more

[Solved] Initializer blocks [closed]

Yes , it will print : main initializer Your initializer will be called once you call the constructor of the c2.Your default constructor for class c2 implicitly looks like : c2() { { System.out.println(“initializer”); } } Refer the JLS for 12.4.2. Detailed Initialization Procedure. 1 solved Initializer blocks [closed]

[Solved] How to obtain parts of a string [closed]

You can use split() and split according to whitespace(s). String str = “add John Do 123-456-789”; String[] res = str.split(“\\s+”); System.out.println(Arrays.toString(res)); Now the result will be: [add, John, Do, 123-456-789] Now you can obtain the string values from res array. As stated in the comments, I suggest you to do a little research before posting … Read more

[Solved] What is a Random object seed [duplicate]

It’s not just a java thing. It’s very hard to let a computer generate a real random number. Your computer needs to perform a complex unpredicatble calculation. Your seed value will act as an input for these calculations. A lot of systems will use a timestamp as a seed. Because that’s a value that will … Read more

[Solved] Chain of responsibility automatic generator [closed]

File folder = new File(“JavaClassesPath”); ArrayList<String> all = new ArrayList<>(); for (final File fileEntry : folder.listFiles()) { if (!fileEntry.isDirectory()) { all.add(fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf(‘.’))); } } String className = “PackageName”; for (String s: all) { if (!s.equals(“AbstractClassName”)) { Class<?> clazz = Class.forName(className + ‘.’ + s); Constructor<?> ctor = clazz.getConstructor(); Object object = ctor.newInstance(); allHandlers.add((RequestHandler) object); } … Read more

[Solved] Is this structure valid? [closed]

The ternary conditional expression produces a value. You must do something with that value (assign it to a variable, return it from your method, etc…). In your case, you are assigning to one of two different variables based on a condition. It doesn’t make much sense and a normal if-else would be more readable: int … Read more

[Solved] How to solve expression with /=

482/10/5/2.0*2+14/5 48/5/2.0*2+14/5 9/2.0*2+14/5 4.5*2+14/5 9.0+14/5 9.0+2 11.0 Divisions go from left to right, addition comes last. An integer is only converted to double in an arithmetic operation with a double. That only happens three times: In step 3 (9/2.0), step 4 (4.5*2) and step 6 (9.0+2). All other operations are pure integer operations. 0 solved … Read more