Found 2616 Articles for Java

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

Maruthi Krishna
Updated on 11-Oct-2019 06:44:29

5K+ Views

Using the parseDouble() methodThe 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 into a double value you, it throws a NumberFormatException.Therefore, to know whether a particular string is parse-able to double or not, pass it to the parseDouble method and wrap this line with try-catch block. If an exception occurs this indicates that the given String is not pars able to double.Example Live Demoimport ... 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

271 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

1K+ 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?

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

12K+ 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.Syntaxpublic @interface JsonIgnoreExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonIgnoreAnnotationTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp = new Employee();       emp.setFirstName("Raja");       emp.setLastName("Ramesh");       emp.setEmpId(120);       emp.getTechnologies().add("Java");       emp.getTechnologies().add("Scala");       emp.getTechnologies().add("Python");       ... Read More

Insert record in a MySQL table with Java

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

598 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

Maruthi Krishna
Updated on 10-Oct-2019 11:21:16

5K+ Views

Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. For example: 7 = 1 × 7Following is the algorithm to find whether a number is prime or not −Take integer variable A.Divide the variable A with (A-1 to 2).If A is divisible by any value (A-1 to 2) it is not prime.Else it is prime.ExampleFollowing Java program accepts an integer from the user, finds whether the given number is prime and, prints the next ... Read More

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

Maruthi Krishna
Updated on 10-Oct-2019 11:18:00

8K+ Views

The filed named out of the System class represents a standard output Stream, an object of the PrintStream class.The println() method of this accepts any a value ( of any Java valid type), prints it and terminates the line.By default, console (screen) is the standard output Stream (System.in) in Java and, whenever we pass any String value to System.out.prinln() method, it prints the given String on the console.Redirecting System.out.println()The setOut() method of the System class in java accepts an object of the PrintStream class and makes it the new standard output stream.Therefore, to redirect the System.out.println() output to a file ... Read More

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

Maruthi Krishna
Updated on 10-Oct-2019 11:15:43

1K+ Views

Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories as −Where, SampleDirectory1 contains two files named SampleFile1.txt and SampleFile2.txt.SampleDirectory2 contains two files named SampleFile2.txt and SampleFile3.txt.ExampleFollowing Java example deletes all the files in the directory named ExampleDirectory.import java.io.File; import java.io.IOException; public class DeletingAllFiles {    public static void deleteFiles(File dirPath) {       File filesList[] = dirPath.listFiles();       for(File file : filesList) {          if(file.isFile()) {             file.delete();          } else {             ... Read More

Java program to List all files in a directory recursively

Maruthi Krishna
Updated on 10-Oct-2019 11:12:54

4K+ Views

Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories as −Where, SampleDirectory1 contains two files named SampleFile1.txt and SampleFile2.txt.SampleDirectory2 contains two files named SampleFile2.txt and SampleFile3.txt.ExampleFollowing Java example lists the names of all the files in the directory named ExampleDirectory.import java.io.File; import java.io.IOException; public class ListOfFiles {    public static void listOfFiles(File dirPath){       File filesList[] = dirPath.listFiles();       for(File file : filesList) {          if(file.isFile()) {             System.out.println("File path: "+file.getName());          } else {     ... Read More

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

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

918 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