Found 7442 Articles for Java

How to check that a string is parse-able to a double in java?

Maruthi Krishna
Updated on 01-Sep-2025 14:10:42

6K+ Views

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

How to read the contents of a web page without using any external library in Java?

Maruthi Krishna
Updated on 11-Oct-2019 06:38:41

378 Views

The 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 passing the URL of the desired web page as a parameter to its constructor.Invoke the openStream() method and retrieve the InputStream object.Instantiate the Scanner class ... Read More

When can we call @JsonAnyGetter and @JsonAnySetter annotations in Java?

raja
Updated on 06-Jul-2020 12:14:04

2K+ Views

The @JsonAnyGetter annotation enables to use a Map as a container for properties that we want to serialize to JSON and @JsonAnySetter annotation instructs Jackson to call the same setter method for all unrecognized fields in the JSON object, which means that all fields that are not already mapped to a property or setter method in the Java object.Syntaxpublic @interface JsonAnyGetter public @interface JsonAnyGetterExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonAnyGetterAndJsonAnySetterTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp1 = new Employee();       emp1.setFirstName("Adithya");       emp1.setLastName("Sai"); ... Read More

How can we ignore the fields during JSON serialization in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:31:48

14K+ Views

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization. In this article, we will learn how to ignore the fields during JSON serialization in Java using the Jackson library. Steps to ignore the fields during JSON serialization in Java: In order to use Jackson, you will need to add it to your project. If you use Maven, add the following dependency to your ... Read More

Insert record in a MySQL table with Java

AmitDiwan
Updated on 17-Feb-2020 06:56:12

821 Views

Let us first create a table. Following is the query to create a table in MySQL −mysql> create table DemoTable(    Id int,    Name varchar(30),    CountryName varchar(30),    Age int ); Query OK, 0 rows affected (0.66 sec)Following is the Java code to access MySQL database −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; public class AccessMySQLDatabase {    public static void main(String[] args) {       Connection con = null;       Statement st = null;       try {          con = DriverManager.getConnection("jdbc :mysql ://localhost :3306/web?" + "useSSL=false", "root", "123456"); ... Read More

Java program to check for prime and find next Prime in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:54:06

6K+ Views

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

Redirecting System.out.println() output to a file in Java

Aishwarya Naglot
Updated on 01-Sep-2025 13:54:55

9K+ Views

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

Java program to delete all the files in a directory recursively (only files)

Aishwarya Naglot
Updated on 01-Sep-2025 13:56:02

2K+ Views

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

Java program to List all files in a directory recursively

Aishwarya Naglot
Updated on 12-May-2025 10:04:59

5K+ Views

The given task is to list all the files from a specific directory, recursively, which means we need to retrieve all the files in the main directory and all the subdirectories as well. For example, if we have a directory structure like below - MyDirectory / \ / ... Read More

What is the easiest way to reverse a String in Java?

Maruthi Krishna
Updated on 05-Dec-2023 09:29:13

1K+ Views

Built-in reverse() method The StringBuffer class provides you a method named reverse(). It reverses the contents of the current StringBuffer object and returns the resultant StringBuffer object. It is the easiest way to reverse a Sting using Java. To do so − Instantiate the StringBuffer class by passing the required String as a parameter. Invoke the reverse() method od the created object. Convert it into String again using the toString() method. Example public class Sample { public static void main(String args[]) { String str = new String("Hello how are you"); StringBuffer sb = new ... Read More

Advertisements