Category: Java

How to convert Array to List in Java ?

This article will explain how to convert a simple array to list in Java . How to convert Array to List in Java ? The Java’s array class provides necessary methods to convert the array to the List using the asList method. Below is a sample code snippet demonstrating the usage of the Arrays.asList method to convert array to List.

Command Line Parsers for Java Developers

There are plenty of command line parsers available for the java developers. Below is the list of some of them. Command Line Parsers for Java Developers JCommando jopt-simple Commons CLI JSAP v2.1: the Java Simple Argument Parser GNU getopt – Java port JArgs Command Line Option Java Gems Natural CLI JewelCli

Java – How to Read Integers from the Console for calculation ?

Problem Statement You need to read integers from the console window and use it for calculation within your java program. Solution If you want to know how to read integer value from a console and use it for calculation , you can use the Scanner class as shown below. The output of the above program is Please enter the first number for abundantcode 10 Please…

Java – How to create BigDecimal from string value ?

Problem Statement You want to create a BigDecimal value from a string type in Java. Solution Use the BigDecimal class and pass the string value in the constructor to create the BigDecimal value as shown below. The output of the above program is Abundantcode.com Java Tutorials 198654586.297292792

For Each loop in Java

Java 5 brought in the support in the support for foreach loop that lets the developers to iterate over collections pretty easily. The syntax for the foreach loop in java looks like the one shown below. for (<datatype> item: collection) { // Process the item } For Each loop in Java Below is an example that demonstrates foreach loop in java. package com.abundantcode; import java.util.ArrayList;…

Java – How to convert String to Double in Java ?

Problem Statement You want to convert a string value to double in Java. Solution You can use one of the three approaches to convert a string to double in Java. 1. Assigning the string to the constructor of the Double class 2. Using Double.valueOf 3. Using Double.parseDouble The output of the above program is Abundantcode.com Java Tutorials 76.2 String to double using valueOf 76.2 string…

InvalidModuleDescriptorException when running java program in Eclipse IDE

When you run your first Java program, there are times when you will receive the below error Error occurred during initialization of boot layer java.lang.module.FindException: Error reading module: C:\Users\Senthil\eclipse-workspace\HelloWorld\bin Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module) The fix for this error is simple. Just delete the module-info.java and run your Main.java file and you should see the program…

How to concatenate two arrays in Java?

There are many ways in which one can concatenate two arrays in Java. Below is a sample soucrecode demonstrating one of the techniques to concatenate arrays in Java. How to concatenate two arrays in Java?

Java – How to read a valid Integer number from Console ?

Problem Statement You need to read a valid integer number from the Console in your java program. If the number is invalid , show appropriate message. Solution To read a valid integer from a console program , you can use the Scanner class and pass the System.in as the parameter to its constructor. The scanner class exposes a method called nextInt() which can be used…

Java – How to convert Long to other primitive numeric data types ?

Problem Statement You need convert a long value to other numeric data types in java. Solution Use the Long wrapper class which exposes methods like byteValue , shortValue , intValue etc. Below is a sample code snipper demonstrating the usage of the same. The output for the above program is Abundantcode.com Java Tutorials 76 76 76.0

Java How to – Compile and run Java program in Command-line

Problem Statement You need to compile and run your java program in command-line. Solution You can use the command-line tools available with Java Development Kit (JDK). The “javac” command tool can be used to compile the java program. The “java” command can be used to run the java program. Let’s build out first program and use the command line to run. 1. Download and install…

Java – How to convert a double value to string ?

Problem Statement You need to convert a double value to string in your java program. Solution Use the toString() method defined in the Double class to convert a double value to string in java. The output of the above program is Abundantcode.com Java Tutorials 25.0

Do-While Loop example in Java

The do-while loop in java is similar to the while loop except that the do-while loop is guaranteed to execute at least once . The do while loop evaluates its condition at the bottom of the loop. The syntax of the do-while loop in java can be written as do{statement(s)}while(expression) Do-While Loop example in Java Below is a sample code snippet demonstrating the usage of…

Break Statement in Java

Java provides the break keyword that is used by the developers to exit out of the loop before the actual completion of the loop. Additionally , the break keyword can also be used to jump out of a single case in switch statement. Break Statement in Java Below is a sample code snippet demonstrating the usage of the break statement in java.  

Java – How to Negate a BigDecimal in Java ?

Problem Statement You need to negate the value of the BigDecimal datatype in your java program. Solution Use the negate method defined in the BigDecimal class as shown below. The output of the above program is Abundantcode.com Java Tutorials -10102