Programming Articles - Page 2397 of 3363

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna
Updated on 01-Sep-2025 12:30:57

896 Views

What is split() method in Java? The split() method of the String class accepts a delimiter (in the form of a string), divides the current String into smaller strings based on the delimiter, and returns the resulting strings as an array. If the String does not contain the specified delimiter, this method returns an array that contains only the current string. If the String does not contain the specified delimiter, this method returns an array containing the whole string as an element. Splitting the string with white space as delimiter Following are steps to split a String into an array ... Read More

How to convert Java object to JSON using Jackson library?

Maruthi Krishna
Updated on 06-Sep-2023 11:49:04

51K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Jackson is a simple java based library to serialize java objects to JSON and vice versa.Converting Java object to JSONThe ObjectMapper class of the Jackson API in Java provides methods to convert a Java object to JSON object and vice versa.The writeValueAsString() method of this class accepts a JSON object as a parameter and returns its respective JSON StringTherefore, ... Read More

How to convert Java object to JSON using GSON library?

Maruthi Krishna
Updated on 10-Oct-2019 06:35:44

2K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google.Converting Java object to JSONThe Google's Gson library provides a class with the same name (Gson) which is the main class of the library.This class provides a method named toJson() there are several variants of this ... Read More

In how many ways we can convert a String to a character array using Java?

Maruthi Krishna
Updated on 10-Oct-2019 06:32:04

363 Views

You can convert a String to a character array either by copying each element of the String to an array or, using the toCharArray() method.Copying each elementGet the String to be converted.Create an empty character array with the length of the String.The charAt() method of the String class returns the character at a particular position. Using this method copy each character of the String to the array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class StringToCharArray {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a String value: ");     ... Read More

Convert CSV to JSON using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:44:03

8K+ Views

A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.In the below example, convert a CSV to JSON.Exampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest {    public static void main(String args[]) throws Exception {       File input = new File("input.csv");       try { ... Read More

How to deserialize a JSON array to list generic type in Java?

raja
Updated on 06-Jul-2020 11:38:01

2K+ Views

The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.Syntaxpublic class TypeToken extends ObjectWe can deserialize a JSON array to a generic type of list in the below exampleExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class JSONArrayToListTest {    public static void main(String args[]) throws Exception {       String jsonStr = "[{\"name\":\"Adithya\", \"course\":\"Java\"}, " + "{\"name\":\"Ravi\", \"course\":\"Python\"}]";       Type listType = new TypeToken() {}.getType();       List students = ... Read More

Convert POJO to XML using the Jackson library in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:35:36

7K+ Views

Jackson is a library that allows you to convert Java objects into XML and vice versa. In this example, we will demonstrate how to convert a POJO (Plain Old Java Object) into XML using the Jackson library. Well, if you are not familiar with POJO, it is a simple Java object that does not follow any specific framework or design pattern. It is just a regular Java class with fields and methods. We will use the method writeValueAsString() of the XmlMapper class to convert a POJO into XML. The XmlMapper class is part of the Jackson library and it ... Read More

C/C++ Pointer Puzzle?

sudhir sharma
Updated on 07-Oct-2019 08:19:16

466 Views

A Pointer is a variable that stores the address of another variable. The data type of the pointer is the same as the data type as the variable.In this puzzle you need to know the size of the pointer that is being used. The puzzle checks our understanding of pointers by asking you the size of variable.The size of int is 4 bytes, whereas the size of int pointer is 8. Now, let’s test your skills with the following exercise in c++ programming language.Example Live Demo#include using namespace std; int main() {    int a = 6 ;    int ... Read More

C/C++ Function Call Puzzle?

sudhir sharma
Updated on 07-Oct-2019 08:14:43

257 Views

This C/C++ function call puzzle is a puzzle that is intended to explore more about the behaviour of method calling in both the programming languages C and C++/.The output of a method in C and C++ is different. Lets see what is the difference in calling methods in C and C++.Let’s take an example and check the output of the below code in c and c++.Example Live Demovoid method() {    // Print statement } int main() {    method();    method(2); }OutputFor C++ −Error : too many arguments to function ‘void method()’For C −Program runs without any error.Logic behind the ... Read More

C++ Boolean Matrix

sudhir sharma
Updated on 07-Oct-2019 08:12:21

1K+ Views

Boolean matrix is a matrix that has only two elements 0 and 1. For this boolean Matrix question, we have a boolean matrix arr[m][n] of size mXn. And the condition to solve is, if m[i][j] = 1 then m[i] = 1 and m[j] = 1 which means all elements of the ith row and jth column will become 1.Let’s take an example, Input: arr[2][2] = 1 0                   0 0 Output: arr[2][2] = 1 1                   1 0Explanation− arr[0][0] = 1 ... Read More

Advertisements