How-to articles, tricks, and solutions about JAVA

How to check if a string contains only digits in Java

To check if a string contains only digits in Java, you can use the matches() method of the String class in combination with the regular expression "\\d+".

"No X11 DISPLAY variable" - what does it mean?

"No X11 DISPLAY variable" is an error message that you might see when trying to run a Java program that uses the X Window System to display a graphical user interface (GUI).

How can I make Java print quotes, like "Hello"?

To print quotes in Java, you need to use the escape character \ to indicate that the quote is part of the string and not the end of the string.

Import Maven dependencies in IntelliJ IDEA

To import Maven dependencies in IntelliJ IDEA, you can follow these steps:

How to determine day of week by passing specific date?

To determine the day of the week for a specific date in Java, you can use the get() method of the Calendar class.

Get specific ArrayList item

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

javac not working in windows command prompt

If javac is not working in the Windows command prompt, there could be several reasons for this.

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.

get string value from HashMap depending on key name

To get the string value from a HashMap depending on the key name in Java, you can use the get() method of the Map interface.

How to convert a Java 8 Stream to an Array?

To convert a Java 8 Stream to an array, you can use the toArray() method of the Stream interface.

How do I get the file name from a String containing the Absolute file path?

To get the file name from a String containing the absolute file path in Java, you can use the following methods:

Why is there no SortedList in Java?

There is no SortedList class in the Java standard library because the List interface already provides a way to store elements in a specific order.

Use Mockito to mock some methods but not others

Mockito is a Java mocking framework that allows you to create mock objects for testing.

How to convert a Collection to List?

In Java, you can convert a Collection (such as Set, Queue, etc.) to a List using the following methods:

How to check if an int is a null

In Java, an int is a primitive data type and cannot be null.

Download a file with Android, and showing the progress in a ProgressDialog

To download a file with Android and show the progress in a ProgressDialog, you can use the following steps:

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.

Java 256-bit AES Password-Based Encryption

In Java, you can use the javax.crypto package to perform 256-bit AES (Advanced Encryption Standard) encryption.

Eclipse error ... cannot be resolved to a type

If you are seeing an error in Eclipse that says "cannot be resolved to a type," it usually means that Eclipse is unable to find the class or interface that you are trying to use in your code.

Using context in a fragment

To use context in a fragment, you will need to provide enough information within the fragment itself for the reader to understand the context.

Any shortcut to initialize all array elements to zero?

To initialize all elements of an array to zero in Java, you can use the Arrays.fill method from the java.util package.

How do I tell Gradle to use specific JDK version?

To specify the JDK version that Gradle should use, you can include the org.gradle.java.home property in the gradle.properties file in the root project directory.

Read/Write String from/to a File in Android

To read and write a string from and to a file in Android, you can use the FileInputStream and FileOutputStream classes along with the InputStreamReader and OutputStreamWriter classes.

How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session

The LazyInitializationException in Hibernate is thrown when an object that has been loaded with a "lazy" fetch type is accessed outside of a valid session.

Eclipse comment/uncomment shortcut?

In Eclipse, you can use the following keyboard shortcuts to comment and uncomment lines of code: