Found 7442 Articles for Java

How to auto-increment the property of a JSONObject in Java?

raja
Updated on 06-Jul-2020 13:03:45

1K+ Views

A JSONObject is an unordered collection of name/value pairs and parses text from a String to produce a map-like object. However, we can auto-increment the property of a JSONObject using the increment() method of JSONObject class. If there is no such property, create one with a value of 1. If there is such a property and if it is an Integer, Long, Double or Float then add one to it.Syntaxpublic JSONObject increment(java.lang.String key) throws JSONExceptionExampleimport org.json.JSONException; import org.json.JSONObject; public class IncrementJSONObjectTest {    public static void main(String[] args) throws JSONException {       JSONObject jsonObj = new JSONObject();       jsonObj.put("year", 2019);     ... Read More

Convert JSONObject to/from a cookie in Java?

raja
Updated on 06-Jul-2020 13:05:15

1K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. We can convert a JSONObject to cookie using the toString() method and convert a cookie to JSONObject using the toJSONObject() method of org.json.Cookie class.Convert JSONObject to cookieSyntaxpublic static java.lang.String toString(JSONObject jo) throws JSONExceptionExampleimport org.json.Cookie; import org.json.JSONObject; public class JSONObjectToCookieTest {    public static void main(String args[]) {       JSONObject jsonObject = new JSONObject();       jsonObject.put("path", "/");       jsonObject.put("expires", "Thu, 07 May 2020 12:00:00 UTC");       jsonObject.put("name", "username");       jsonObject.put("value", "Adithya");       String cookie = Cookie.toString(jsonObject); ... Read More

What is ArrayStoreException in Java?

Maruthi Krishna
Updated on 15-Oct-2019 10:44:49

615 Views

When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double value in it. Live Demoimport java.util.Arrays; public class ArrayStoreExceptionExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 12548;       integerArray[1] = 36987;       integerArray[2] = 555.50;       integerArray[3] = 12548;     ... Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Updated on 08-May-2025 11:52:38

4K+ Views

No, HashMap does not allow duplicate keys. The HashMap is a class that implements the Map interface. It is based on the Hash table. It is used to store key-value pairs. It allows null keys and values, but it does not allow duplicate keys. You can store key-value pairs in the HashMap object. Once you do so, you can retrieve the values of the respective keys, but the values we use for keys should be unique. Let's understand what will happen if we try to add a duplicate key into a HashMap.Addng a duplicate key to a HashMapThe put() method ... Read More

Difference between ArrayList.clear() and ArrayList.removeAll() in java?

Aishwarya Naglot
Updated on 08-May-2025 11:56:44

1K+ Views

The ArrayList class in Java is a Resizable-array implementation of the List interface. It allows null values. Java clear() Method V/S removeAll() Method There are some important differences between the clear() and removeAll (Collection c) methods of the ArrayList class. This table compares these two methods. Key clear() removeAll() ... Read More

How to convert a JSON to Java Object using the Jackson library in Java?

Manisha Chand
Updated on 19-May-2025 15:51:31

42K+ Views

Jackson is a Java library that is used to convert JSON to Java objects and vice versa. Conversion of JSON to a Java object is called deserialization, and Java object to JSON is known as serialization. Both of these tasks can be done by using the Jackson library. In this article, we are going to learn how to convert JSON to Java objects using the Jackson library. Jackson Library: Convert a JSON to a Java Object The ObjectMapper class belongs to the Jackson library. This class is responsible for the serialization and deserialization of Java objects. The ObjectMapper class is used ... Read More

Reading data from keyboard using console class in Java

Maruthi Krishna
Updated on 08-May-2025 10:00:29

834 Views

The Console class is part of the Java.io package and is used to read input from the keyboard or user and write output to the console. Unlike Scanner, the Console class provides methods to read text and passwords. If you read a password using the Console class, it will not be displayed to the user (without showing the typed characters). To use the Console class, we need its object, and the Console object is obtained by calling System.console() as shown below - Console console = System.console(); Note: If you try to execute the program in a non-interactive environment like an IDE, it doesn’t work. It ... Read More

How to create a directory hierarchy using Java?

Maruthi Krishna
Updated on 12-Mar-2024 18:37:31

2K+ Views

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories. The mkdir() method of this class creates a directory with the path represented by the current object. Creating directory hierarchy There are two main ways to create a directory hierarchy in Java − Using mkdirs() Method Using createDirectories() Method Let us look at these solutions one by one. Using mkdirs() Method To create a hierarchy of new directories you can using the method mkdirs() of the same class. This ... Read More

Converting a StringBuilder to String in Java

Maruthi Krishna
Updated on 02-Sep-2023 15:06:14

59K+ Views

The toString() method of the StringBuilder class reruns String value of the current object. To convert a StringBuilder to String value simple invoke the toString() method on it.Instantiate the StringBuilder class.Append data to it using the append() method.Convert the StringBuilder to string using the toString() method.ExampleIn the following Java program we are converting an array of Strings to a single String using the toString() method of the StringBuilder. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder(); ... Read More

Converting String to StringBuilder in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:38:52

647 Views

The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings to a single StringBuilder object. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder();       sb.append(strs[0]);       sb.append(" "+strs[1]);       sb.append(" ... Read More

Advertisements