[Solved] I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray. How can I do it?

You can try with JSONSimple library and form the object in this way- You have to import org.json.simple.JSONArray and org.json.simple.JSONObject for using this code. JSONObject object=new JSONObject(); JSONObject holder=new JSONObject(); JSONArray taskAssings = new JSONArray(); JSONObject taskAssigned=new JSONObject(); taskAssigned.put(“id”, “3c814009-82f7-4246-bc51-2d263e758561”); JSONObject taskAssignee=new JSONObject(); taskAssignee.put(“id”, “3c814009-82f7-4246-bc51-2d263e758561”); holder.put(“taskAssigned”,taskAssigned); holder.put(“taskAssignee”,taskAssignee); taskAssings.add(holder); object.put(“taskAssings”, taskAssings); JSONObject status=new JSONObject(); status.put(“id”, “7d8a0d80-5c93-46cc-982d-47399503beaa”); … Read more

[Solved] String index out of range: -5 [closed]

You have misunderstood what argument Java’s String.substring function takes. In short, you appear to think the argument takes the length of the substring. It doesn’t – rather, it specifies the “begin-index” – ie. where in the supplied string to start copying. So when you say : final String year = “yyyy “; args[0] = year.substring(5); … Read more

[Solved] How to get time from user with respect to timezone

String userTimeZone = “Asia/Samarkand”; String userDate = “2018-07-05”; ZoneId zone = ZoneId.of(userTimeZone); Instant dbInstant = LocalDate.parse(userDate) .atStartOfDay(zone) .toInstant(); System.out.println(dbInstant); This prints what you had expected: 2018-07-04T19:00:00Z I don’t know MongoDB’s JDBC driver, but I assume it would be happy to accept an Instant and store it in UTC in the database. GMT+05:00 is not really … Read more

[Solved] Subtracting months in Java [closed]

The simplest approach is to use ChronoUnit#between method: DateTimeFormatter formatter = DateTimeFormatter.ofPattern( “MMMM dd, yyyy”, Locale.US ); LocalDate d1 = LocalDate.parse( “May 28, 2019”, formatter ); LocalDate d2 = LocalDate.parse( “September 02, 2020”, formatter ); long diff = ChronoUnit.MONTHS.between( d1, d2 ); // 15 full months 1 solved Subtracting months in Java [closed]

[Solved] Creation of JavaRDD object has failed

You just have to instantiate JavaSparkContext. SparkConf conf = new SparkConf(); conf.setAppName(“YOUR APP”); //other config like conf.setMaster(“YOUR MASTER”); JavaSparkContext ctx = new JavaSparkContext(conf); //and then JavaRDD<Row> testRDD = ctx.parallelize(list); 1 solved Creation of JavaRDD object has failed

[Solved] how to fix Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: class javax.swing.JLabel cannot be cast to class java.lang.String

You get the exception because you are trying to cast a JLabel to String. at row=0 cloumn=0 is a JLabel and I want to get value of JLabel and put in query Display((String) table.getValueAt(0, 0)); If you want the text-content of a label, use JLabel.getText() instead: JLabel label = (JLabel)table.getValueAt(0, 0); String text = label.getText(); … Read more

[Solved] How to fix it ? i am very very new to java

Try this code. It is working fine import java.util.Scanner; import java.util.UUID; public class AccountThis { private static Scanner scanner; public AccountThis(String nameInput) { System.out.println(” created account with name ” + nameInput); } public AccountThis() { System.out.println(“created an account.”); } public AccountThis(double depositInput) { System.out.println(depositInput + “$” + “added to your account!”); } public AccountThis(String nameInput, … Read more

[Solved] Java – Moving all sub-directory files to Parent Directory

private static void move(File toDir, File currDir) { for (File file : currDir.listFiles()) { if (file.isDirectory()) { move(toDir, file); } else { file.renameTo(new File(toDir, file.getName())); } } } Usage: pass it parent directory (ex. move(parentDir, parentDir)). 1 solved Java – Moving all sub-directory files to Parent Directory

[Solved] looping given max and range

public static void loopingIssue(Integer totalItems, Integer range) { IntStream.range(0, totalItems).filter(i -> i % range == 0) .mapToObj(e -> mapToGroup(e, totalItems, range)) .forEach(System.out::print); } public static String mapToGroup(Integer e, Integer totalItems, Integer maxRange) { if (e + maxRange >= totalItems) { return e + “-” + (totalItems – 1); } else { return e + “-” … Read more

[Solved] I couldn’t make this programm run as I wanted

Your code has few errors. From Usage: java Phone it looks like expected content of args array should be “Phone” <name> which are two elements so if(args.length != 1) is not valid condition. You probably should replace it with if (args.length < 2) Other problem is that <name> is second element in args array stored … Read more