How-to articles, tricks, and solutions about JAVA

Regex pattern including all special characters

To create a regular expression that includes all special characters, you can use the following pattern:

Remote debugging a Java application

To remotely debug a Java application, you need to start the application with the java command and the -agentlib:jdwp option.

Remove all occurrences of char from string

To remove all occurrences of a particular character from a string, you can use the replace() method. This method takes two arguments: the string to be replaced, and the string to replace it with. If you pass an empty string as the second argument

Remove HTML tags from a String

To remove HTML tags from a string, you can use a regular expression to match and replace the tags with an empty string.

Remove Item from ArrayList

To remove an item from an ArrayList in Java, you can use the remove() method of the ArrayList class.

Remove last character of a StringBuilder?

To remove the last character of a StringBuilder in Java, you can use the deleteCharAt method and pass in the index of the character you want to delete.

Remove part of string in Java

To remove a part of a string in Java, you can use the replace() method of the String class.

Removing an element from an Array in Java

There are a few different ways you can remove an element from an array in Java. Here are a few options:

Removing whitespace from strings in Java

There are a few different ways you can remove whitespace from strings in Java. Here are a few options: Using the replaceAll method:

Replace a character at a specific index in a string?

To replace a character at a specific index in a string in Java, you can use the substring() method of the java.lang.

RESTful call in Java

To make a RESTful call in Java, you can use the HttpURLConnection class.

Returning Arrays in Java

To return an array in Java, you can simply specify the array type as the return type of the method.

Returning JSON object as response in Spring Boot

To return a JSON object as a response in Spring Boot, you can use the @ResponseBody annotation and the ObjectMapper class.

Reverse a string in Java

To reverse a string in Java, you can use the following approaches:

round up to 2 decimal places in java?

To round a number up to two decimal places in Java, you can use the Math.ceil method and multiply the number by 100, then divide the result by 100.

Run a single test method with maven

To run a single test method with Maven, you can use the surefire:test goal and specify the fully-qualified name of the test class and the method name using the test and method properties, respectively.

Running JAR file on Windows

To run a JAR file on Windows, you will need to have the Java Runtime Environment (JRE) installed on your system. You can then run the JAR file by double-clicking it or by using the java command in the command prompt.

Safely casting long to int in Java

In Java, you can safely cast a long value to an int by checking to see if the long value is within the range of the int data type.

Scanner is skipping nextLine() after using next() or nextFoo()?

It is common for the Scanner class's next() and nextFoo() methods (where Foo is any primitive type such as Int, Double, etc.) to skip over newline characters in the input. This is because these methods are designed to read only the next token in the input

Sending Email in Android using JavaMail API without using the default/built-in app

To send an email in Android using the JavaMail API without using the default/built-in app, you can use the following steps:

Sending HTTP POST Request In Java

To send an HTTP POST request in Java, you can use the java.net.URL and java.net.HttpURLConnection classes. Here is an example of how you can use these classes to send a POST request:

Serializing with Jackson (JSON) - getting "No serializer found"?

If you are getting the error "No serializer found" when trying to serialize an object to JSON using Jackson, it usually means that Jackson does not know how to serialize one or more fields in the object.

Set ImageView width and height programmatically?

To set the width and height of an ImageView programmatically, you can use the setLayoutParams() method and pass it a LayoutParams object.

Setting active profile and config location from command line in spring boot

To set the active profile and the configuration location from the command line in Spring Boot, you can use the spring.profiles.active and spring.config.name properties.

Setting default values for columns in JPA

To set default values for columns in JPA (Java Persistence API), you can use the @Column annotation and the columnDefinition attribute.