Found 2616 Articles for Java

How to serialize the order of properties using the Jackson library in Java?

raja
Updated on 06-Jul-2020 12:01:48

3K+ Views

The @JsonPropertyOrder is an annotation to be used at the class-level. It takes as property a list of fields that defines the order in which fields can appear in the string resulting from the object JSON serialization. The properties included in the annotation declaration can be serialized first(in defined order), followed by any properties not included in the definition.Syntaxpublic @interface JsonPropertyOrderExampleimport com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.util.*; import java.io.*; public class JsonPropertyOrderTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException,        IOException {       Employee emp = new Employee();       emp.setFirstName("Adithya");     ... Read More

How to delete a string inside a file(.txt) in java?

Maruthi Krishna
Updated on 10-Oct-2019 09:16:10

6K+ Views

The replaceAll() method accepts a regular expression and a String as parameters and, matches the contents of the current string with the given regular expression, in case of match, replaces the matched elements with the String.To delete a particular String from a file using the replaceAll() method −Retrieve the contents of the file as a String.Replace the required word with an empty String using the replaceAll() method.Rewrite the resultant string into the file again.Exampleimport java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class StringExample {    public static String fileToString(String filePath) throws Exception{       String input = null; ... Read More

How can we add an underscore before each capital letter inside a java String?

Maruthi Krishna
Updated on 04-Dec-2023 11:35:02

2K+ Views

Using the StringBuffer class To add underscore before each capital letter in a String using StringBuffer − Create an empty StringBuffer object. The isUpperCase() method of Character class accepts a character and verifies whether it is in upper case, if so, this method returns true. Using this method, verify each character in the String. In case of upper case letter append underscore before it, using the append() method. Example public class Adding_BeforeCapital { public static void main(String args[]) { String str = "HelloHowAreYouWelcome"; StringBuffer sb = new StringBuffer(); for ... Read More

How to implement a custom serializer using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:52:25

936 Views

The Jackson API provides a number of methods to work with JSON data. By using Jackson API, we can convert Java objects to JSON string and reform the object from the JSON string. We can implement a custom serializer using the StdSerializer class and need to override the serialize(T value, JsonGenerator gen, SerializerProvider provider) method, the first argument value represents value to serialize(can not be null), the second argument gen represents generator used to output resulting Json content and the third argument provider represents provider that can be used to get serializers for serializing objects value.Syntaxpublic abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOExceptionExampleimport java.io.*; ... Read More

How to insert a string in beginning of another string in java?

Maruthi Krishna
Updated on 10-Oct-2019 09:08:29

5K+ Views

Using a character arrayGet the both strings, suppose we have a string str1 and the string to be added at begin of str1 is str2.Create a character array with the sum of lengths of the two Strings as its length.Starting from 0th position fill each element in the array with the characters of str2.Now, from (length of str2)th position to the end of the array fill the character from the 1st array.Exampleimport java.util.Scanner; public class StringBufferExample {    public static void main(String args[]) {       System.out.println("Enter string1: ");       Scanner sc= new Scanner(System.in);       ... Read More

How convert an array of Strings in a single String in java?

Maruthi Krishna
Updated on 14-Sep-2023 02:33:17

29K+ Views

Using StringBufferCreate an empty String Buffer object.Traverse through the elements of the String array using loop.In the loop, append each element of the array to the StringBuffer object using the append() method.Finally convert the StringBuffer object to string using the toString() method.Examplepublic class ArrayOfStrings {    public static void main(String args[]) {       String stringArray[] = {"Hello ", " how", " are", " you", " welcome", " to", " Tutorialspoint"};       StringBuffer sb = new StringBuffer();       for(int i = 0; i

How do we find out if first character of a string is a number in java?

Maruthi Krishna
Updated on 11-Oct-2019 07:58:57

3K+ Views

Using the isDigit() methodThe isDigit() method of the java.lang.Character class accepts a character as a parameter and determines whether it is a digit or not. If the given character is a digit this method returns true else, this method returns false.Therefore, to determine whether the first character of the given String is a digit.The charAt() method of the String class accepts an integer value representing the index and returns the character at the specified index. The toCharArray() method of this class converts the String to a character array and returns it you can get its first character as array[0].Retrieve the 1st character ... Read More

How to read the contents of a webpage into a string in java?

Maruthi Krishna
Updated on 10-Oct-2019 13:44:14

11K+ Views

You can read the contents of a web page in several ways using Java. Here, we are going to discuss three of them.Using the openStream() methodThe URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource (file or, directory or a reference) in the world wide web.The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you can read data from the URL.Therefore, to read data from web page (using the URL class) −Instantiate the java.net.URL class by ... Read More

How to validate given date formats like MM-DD-YYYY using regex in java?

Maruthi Krishna
Updated on 05-Dec-2023 09:18:50

4K+ Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences. The pattern class of this package is a compiled representation of a regular expression. To match a regular expression with a String this class provides two methods namely − compile() − This method accepts a string representing a regular expression and returns an object of the Pattern object. matcher() − This method accepts a String value and creates a matcher object which matches the given String to the pattern represented by the current pattern object. Following is the regular expression to match date in dd-MM-yyyy format − ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$ Therefore, to validate ... Read More

How to Convert a String to Hexadecimal and vice versa format in java?

Maruthi Krishna
Updated on 11-Oct-2019 08:01:08

14K+ Views

String to HexadecimalThe toHexString() method of the Integer class accepts an integer as a parameter and returns a hexadecimal string. Therefore, to convert a string to a hexadecimal String −Get the desired String.Create an empty StringBuffer object.Convert it into a character array using the toCharArray() method of the String class.Traverse through the contents of the array created above, using a loop.Within the loop convert each character of the array into an integer and pass it as a parameter to the toHexString() method of the Integer class.Append the resultant values to the StringBuffer object using the append() method of the StringBuffer ... Read More

Advertisements