[Solved] create a JSON file in java with array

A little helper makes life easier: public static JSONArray jsonArray(Object… values) { JSONArray arr = new JSONArray(); arr.addAll(Arrays.asList(values)); return arr; } Then: JSONObject obj = new JSONObject(); obj.put(“data”, jsonArray(jsonArray(“1”, “YES”, “sp_1”, “1”, “xxx”), jsonArray(“2”, “NO” , “sp_2”, “2”, “yyyy”), jsonArray(“3”, “YES”, “sp_3”, “2”, “zzzz”))); System.out.println(obj.toJSONString()); Output {“data”:[[“1″,”YES”,”sp_1″,”1″,”xxx”],[“2″,”NO”,”sp_2″,”2″,”yyyy”],[“3″,”YES”,”sp_3″,”2″,”zzzz”]]} 1 solved create a JSON file in java … Read more

[Solved] How to iterate over Array List and modify elements inside the object in Java?

Assuming that class CatchesItem has time field defined as LocalDateTime, the check for today’s timestamp may be implemented as follows: import java.time.*; // … LocalDate today = LocalDate.now(); for (CatchesItem item : items) { if (today.equals(item.getTime().toLocalDate())) { item.setAmount(item.getAmount() + 5); // add some value to amount } } items.forEach(System.out::println); Output (for the input data as … Read more

[Solved] How to change IP address from binary to dotted decimal notation? [closed]

you should get an integer from binary (pareseInt method) and contact all of them by “.”: System.out.println( Integer.parseInt(“00000011”, 2) + “.” + Integer.parseInt(“10000000”, 2) + “.” + Integer.parseInt(“11111111”, 2) + “.” + Integer.parseInt(“11111111”, 2) ); Output: 3.128.255.255 6 solved How to change IP address from binary to dotted decimal notation? [closed]

[Solved] Get specific information from text file

You could use the split function of a string. Therefore you read in the complete line into a string variable, e.g. line, and then use line.split(” “) with a space as argument. This will return an array containing both values, of which you can proceed with the second. For further information: Java String.split() solved Get … Read more

[Solved] Time Complexity of remove method of Set

Depending on the underlying implementation a remove in a List / Set can be O(1), O(n) or O(log n) (or some other even bigger complexity for esoteric implementations). ArrayList remove is O(n) on everything but the last element. Removing the last element in an ArrayList is O(1) (decrementing the size counter and setting the element … Read more

[Solved] How is java byte code executed since all operating systems don’t preinstalled JRE(JAVA RUNTIME ENVIRONMENT) that include java virtual machine [closed]

How is java byte code executed since all operating systems don’t preinstalled JRE(JAVA RUNTIME ENVIRONMENT) that include java virtual machine [closed] solved How is java byte code executed since all operating systems don’t preinstalled JRE(JAVA RUNTIME ENVIRONMENT) that include java virtual machine [closed]

[Solved] NullPointerException error in Insertion sort [closed]

You are not passing the array object to sort method. Try the following code: public class SortTest { public void sort(String[] array) { String insert; for (int next = 1; next < array.length; next++) { insert = array[next]; int moveItem = next; while (moveItem > 0 && array[moveItem – 1].compareTo(insert) > 0) { array[moveItem] = … Read more

[Solved] Java Square Class [closed]

Change your loop into this: for (int i=0; i<width; i++) { for (int j=0; j<width; j++) { System.out.print(“*”); } System.out.println(); } As said, remove semicolons after loops because this makes your loop end immediately without arguments. i should be set to 1 so that it would reiterate in the correct number of times And edit … Read more

[Solved] Convert Base64 String to String Array

The jsondata value is JSON text. It starts with [, which means it’s a JSON array. To process it, you should use a JSON parser. See How to parse JSON in Java. Once you have parsed it, you should have a String[] or a List<String>, with 2 values. Both values start with data:image/jpeg;base64, followed by … Read more

[Solved] Does this work for anyone? [closed]

Don’t override the paint() method. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the applet Don’t invoke repaint() from any painting method. This will cause an infinite loop. Don’t use sleeping code inside a painting method. If you want animation then use … Read more

[Solved] Does HashMap gives output in the same order as the order in which the data is added to it?

No. Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in … Read more