Found 9150 Articles for Object Oriented Programming

Sorting contents of a string that holds integer values in Java

Aishwarya Naglot
Updated on 01-Sep-2025 12:50:16

4K+ Views

Problem Statement Here we have a string that contains integer values, our task is to sort those integer values in ascending order. For example, if the string is "31134", the sorted string should be "11334". Solution We can solve the above problem using multiple ways: Using char array Using Stream API Using Bubble Sort Using char array We will use the toCharArray() method of the String class to convert the string into a character array. Then we will sort the character array using the Arrays.sort() method. ... Read More

How to check if String value is Boolean type in java?

Maruthi Krishna
Updated on 11-Oct-2019 07:00:42

12K+ Views

The Boolean class of the lang package provides two method namely parseBoolean() and valueOf().parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.valueOf(String s) − This method accepts a String value, parses it and returns an object of the Boolean class based on the given value. You can use this method instead of the constructor. If the given String value is "true" this method returns true or, it returns ... Read More

How to execute an external program like windows media player in Java?

Maruthi Krishna
Updated on 11-Oct-2019 06:56:18

1K+ Views

Using the Runtime classJava provides a class named java.lang.Runtime, using this class you can interface with the current environment.The getRunTime() (static) method of this class returns a Runtime object associated with the current application.The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.Therefore, to execute an external application using the Runtime class −Get the run time object using the getRuntime() method.Execute the required process by passing the path of it as a String value to the exec() method.Exampleimport java.io.IOException; public class Trail {    public static void main(String ... Read More

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

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

Advertisements