How to convert float to int in Java?

You can convert a float to an int in Java by using the round method Math.round(float):

The method rounds the float to the closest int value. Other methods that you can use to convert a float to int are: (int) Math.ceil(float) – returns the smallest int which is greater than or equal to the method […]

Initialize String[] in Java

To initialize String[] you can do:

More details are given below. Declaration vs Initialization Let’s consider the array strings:

The above line of code is just a declaration of an array. There is no memory allocated for the elements of strings. If you would try to assign some value to the first element of […]

Java Sorting Map by key

To have a sorted map by key in Java you can use the TreeMap. It guarantees the natural order. You can use it like this:

For more details read the following paragraphs. TreeMap with the natural order This type of a Map stores the elements in the natural order. If you just want to sort a variable […]

How to convert String to Character array in Java

Using ArrayUtils If you are familiar with Apache Commons you can try this code:

The toCharArray() method will converts the String to char array. The ArrayUtils.toObject(String) method to converts the array of chars to the array of Character objects. Own method If for some reason you don’t want to use the Apache solution, you can write your […]

How to get file extension in Java?

Simple Java parser If you want to get a file extension from the file path you can try this code:

The fileName is a variable which contains the name of your file. lastIndexOf() will give you the index of the last dot in a file name. The if statement guarantees that you will get a substring […]

org.apache.maven.plugin.PluginResolutionException: Plugin or one of its dependencies could not be resolved

If you are struggling with the error:

You should check the maven repository server is up firstly. It can be the root of the problem. Of course, sometimes the server is up, and you still get the error. That’s why there are more ways to deal with it in paragraphs below. Set up the […]

How to create an InputStream from a String in Java?

This short post will present how to convert String to InputStream in Java. 1. The String.getBytes() method with ByteArrayInputStream constructor

At the beginning, we declared a String and used the String.getBytes() method to convert it to the bytes array. We created a new InputStream object by using the constructor of the ByteArrayInputStream class, passing the bytes array as […]

How to round a double to n decimal places in Java

To round a double to n decimal places, we need to use the BigDecimal.doubleValue() method. 1. Creating a proper method We can easily reach the rounding by creating the round method, like in the example below.

As you can see, this code allows us to round to a specific number of places. In the above example, […]

How to run a jar file in command prompt

In this very short post, we will show how to run a jar file in command prompt. 1. -jar command To run a jar file you need use this command:

2. -cp command Although, if you don’t have a manifest in your jar invoking java -jar command will not work. In that case, you should use -cp command: