How-to articles, tricks, and solutions about JAVA

How to nicely format floating numbers to string without unnecessary decimal 0's

To format a floating-point number as a string without unnecessary decimal zeros, you can use the DecimalFormat class from the java.text package. The DecimalFormat class allows you to specify a pattern for formatting numbers as strings.

How to open/run .jar file (double-click not working)?

To open a .jar file on a Windows system, you can do one of the following:

How to parse a date?

To parse a date in Java, you can use the SimpleDateFormat class.

How to parse JSON in Java

To parse a JSON string in Java, you can use the org.json library. This library provides a simple and easy-to-use interface for parsing and manipulating JSON data in Java.

How to parse JSON in Kotlin?

To parse JSON in Kotlin, you can use the JSONObject class from the org.json package.

How to pass a function as a parameter in Java?

In Java, you can pass a function as a parameter using a functional interface. A functional interface is an interface that has a single abstract method. You can create a functional interface by annotating an interface with the @FunctionalInterface annotati

How to pass an object from one activity to another on Android

There are several ways you can pass an object from one activity to another on Android:

How to perform mouseover function in Selenium WebDriver using Java?

To perform a mouseover (hover) action in Selenium WebDriver using Java, you can use the Actions class.

How to POST form data with Spring RestTemplate?

To POST form data with the RestTemplate class in Spring, you can use the postForObject method and pass it a URL, an object containing the form data, and the response type.

How to pretty print XML from Java?

To pretty print XML from Java, you can use the javax.xml.transform.Transformer class from the Java XML Transformer API.

How to print a float with 2 decimal places in Java?

To print a float with 2 decimal places in Java, you can use the printf method and specify a format string with the %.2f format specifier. Here's an example:

How to print a query string with parameter values when using Hibernate

To print a Hibernate query string with parameter values, you can use the toString() method of the Query interface.

How to print color in console using System.out.println?

To print colored text to the console using System.out.println, you will need to use special escape sequences in your string to specify the desired color.

How to print out all the elements of a List in Java?

To print out all the elements of a List in Java, you can use a for loop or an enhanced for loop.

How to print to the console in Android Studio?

To print to the console in Android Studio, you can use the Log class from the android.util package.

How to programmatically close a JFrame

To programmatically close a JFrame in Java, you can use the dispose method.

How to put a Scanner input into an array... for example a couple of numbers

To put input from a Scanner into an array in Java, you can use a loop to read the input and store it in the array.

How to read all files in a folder from Java?

To read all the files in a folder from Java, you can use the File class from the java.io package to list all the files in a directory. Here's an example of how you can do this:

How to read and write excel file

There are several ways to read and write Excel files in Java. Here are a few options:

How to read file from relative path in Java project? java.io.File cannot find the path specified

To read a file from a relative path in a Java project, you can use the File class from the java.io package and specify the relative path to the file.

How to read integer value from the standard input in Java

To read an integer value from the standard input (keyboard) in Java, you can use the Scanner class from the java.util package.

How to read json file into java with simple JSON library

To read a JSON file into Java using the Simple JSON library, you can use the JSONObject class and the JSONArray class.

How to read text file from classpath in Java?

To read a text file from the classpath in Java, you can use the getResourceAsStream method of the ClassLoader class to get an InputStream for the file, and then use a BufferedReader to read the contents of the file.

How to read XML using XPath in Java

To read XML using XPath in Java, you can use the javax.xml.xpath package.

How to reference a method in javadoc?

To reference a method in Javadoc, you can use the {@link} tag.