Let's learn how to check if a string is parseable to a double in Java. We have multiple ways to do this. Some of them are: Using Double.parseDouble() Using Double.valueOf() Using the constructor of the Double class Using Double.parseDouble() The parseDouble() method of the java.lang.Double class accepts a String value, parses it, and returns the double value of the given String. If you pass a null value to this method, it throws a NullPointerException and if this method is not able to parse the given string ... Read More
Automatic resource management(ARM) or try-with-resources is a new exception handling mechanism that we are going to discuss in this article. What is a resource? A resource is an object which implements AutoClosable interface. Whenever you use a resource in your program, it is recommended to close it after use. For example, if you open a file using FileInputStream, it is recommended to close the stream after usage. This is done to free up the system resources. In Java, we can use the close() method to close the resources. But if you forget to close the resource, it will lead ... Read More
Let's learn how to add a string at the beginning of another string in Java. We can do this in the following ways: Using the toCharArray(). Using the String concatenation operator. Using the StringBuilder class. Using the StringBuffer class. Using the toCharArray() toCharArray() method of the String class that helps us to convert a string into a character array. We will use this method to insert a string at the beginning of another string. Following are the steps to do so: Get both strings, suppose we have a string str1 and the string to be added at ... Read More
In this article, we will learn how to delete a string inside a file (.txt) in Java. The following are the ways to do so: Using FileOutputStream and replaceAll() method. Using replace() method. Using FileOutputStream The FileOutputStream is a class in the java.io package. It is mainly used for writing data to a file. It creates a file output stream to write data to the specified file. To delete a string inside a file using the FileOutputStream class, follow the steps below: Create a file ... Read More
To delete all the files in a directory recursively (only files), we can use the class Files, which is part of the java.nio.file package. The Files class helps in performing file operations such as creating, deleting, and copying files. Suppose we have a directory named test with the following structure: file1.txt file2.txt file3.txt subdir1 file4.txt file5.txt subdir2 ... Read More
In Java, the System is a class that provides access to the resources called "System resources". It is a final class, which means it cannot be inherited by any other class. It is a part of the java.lang package. The System.out.println() method prints the data on the console. In this article, we will learn how to redirect the output of the System.out.println() method to a file. Following are few of the ways through we achieve this: Using System.setOut() with PrintStream Using FileOutputStream with PrintStream The filed named out of the System class represents a standard output Stream, an ... Read More
In this article, let's learn how to check for a prime and find the next prime number in Java. Following are the different ways to do so: Using the isPrime() method. Using the nextPrime() method. Manual checking for prime and next prime. Using the isProbablePrime() method The isProbablePrime() is method of the java.math.BigInteger class. It accepts an integer value as a parameter, checks if it is prime or not, and returns true if it is prime, else it returns false. To check if a number is prime using the isPrime() method, follow the steps below: Instantiate the ... Read More
When working with text in Java we often need to include new line characters to the format output properly. Different operating systems have different conventions for new line characters: Windows: This uses \r (Carriage Return + Line Feed). Unix/Linux: This uses (Line Feed). Mac (pre-OS X): This uses \r (Carriage Return). To write code that works seamlessly across all platforms and we need to use a platform-independent way of handling new line characters. This article will guide us through the different methods available in the Java to achieve this. Using Platform-Independent New Line Characters The recommended way ... Read More
Jackson is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can format a date using the setDateFormat() of ObjectMapper class. This method can be used for configuring the default DateFormat when serializing time values as Strings and deserializing from JSON Strings. Syntax of setDateFormat() public ObjectMapper setDateFormat(DateFormat df) Here, df is the DateFormat instance to be used for formatting dates. Steps to format a date using Jackson in Java Following ... Read More
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP