How-to articles, tricks, and solutions about JAVA

Cast Double to Integer in Java

In Java, you can cast a double value to an int using the (int) operator. This will truncate the decimal part of the double and return the integer value.

Check and extract a number from a String in Java

To check if a string contains a number and extract the number from the string in Java, you can use a combination of the matches method of the String class and the find method of the Matcher class.

Check if a String contains a special character

To check if a String contains a special character in Java, you can use a regular expression to match any character that is not a letter or a digit.

Check whether a String is not Null and not Empty

To check if a string is not null and not empty in Java, you can use the length() method of the java.lang.String class to check if the string is empty, and the != operator to check if the string is not null. Here is an example of how you can do this:

Check whether number is even or odd

To check whether a number is even or odd in Java, you can use the modulus operator (%) to determine the remainder of the number when it is divided by 2.

Checking if a string is empty or null in Java

To check if a string is empty or null in Java, you can use the isEmpty() method of the java.lang.String class, which returns true if the string is empty, and false if it is not. Here is an example of how you can use isEmpty() to check if a string is empty

Class has been compiled by a more recent version of the Java Environment

If you get the error "class has been compiled by a more recent version of the Java Environment", it means that you are trying to run a class file that was compiled with a newer version of Java than the one you have installed.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure is an exception that is thrown when there is a failure in the communication link between your Java program and the MySQL database. This can happen for a variety of reason

Comparing chars in Java

To compare two char values in Java, you can use the == operator, just like you would with any other primitive type. For example:

Comparing Java enum members: == or equals()?

In Java, it is generally recommended to use the equals() method to compare enum members, rather than the == operator.

Comparing strings by their alphabetical order

To compare strings by their alphabetical order in Java, you can use the compareTo() method of the String class.

Connect Java to a MySQL database

To connect a Java application to a MySQL database, you need to use the JDBC (Java Database Connectivity) API.

console.writeline and System.out.println

console.writeline is not a valid method in Java.

Convert a JSON String to a HashMap

Here is an example of how you can convert a JSON string to a HashMap in Java:

Convert a string representation of a hex dump to a byte array using Java?

To convert a string representation of a hex dump to a byte array in Java, you can use the DatatypeConverter class's parseHexBinary method.

Convert an integer to an array of digits

To convert an integer to an array of digits in Java, you can use the toCharArray() method of the String class to convert the integer to a string, and then use the toCharArray() method to convert the string to an array of characters.

Convert ArrayList<String> to String[] array

To convert an ArrayList<String> to a String[] array in Java, you can use the toArray method of the ArrayList class and pass it an empty array of the appropriate type:

Convert boolean to int in Java

To convert a boolean value to an integer in Java, you can use the intValue() method of the java.lang.Boolean class, or you can use a conditional operator.

Convert character to ASCII numeric value in java

To convert a character to its ASCII numeric value in Java, you can use the charAt method of the String class and the intValue method of the Character class.

Convert float to String and String to float in Java

To convert a float to a String in Java, you can use the Float.toString method or the String.valueOf method. For example:

Convert InputStream to byte array in Java

To convert an InputStream to a byte array in Java, you can use the readAllBytes() method of the java.nio.file.Files class from the java.nio.file package.

Convert int to char in java

To convert an int value to a char value in Java, you can use the char type's conversion method, (char).

Convert java.time.LocalDate into java.util.Date type

To convert a java.time.LocalDate into a java.util.Date type in Java, you can use the atStartOfDay() method of the LocalDate class to get a LocalDateTime object and then use the toInstant() method to convert it to an Instant object.

Convert java.util.Date to java.time.LocalDate

To convert a java.util.Date object to a java.time.LocalDate object, you can use the java.time.Instant class to represent the date as an instant in time, and then use the java.time.LocalDateTime class to convert the instant to a date and time in the local

Convert java.util.Date to String

To convert a java.util.Date object to a String, you can use the format method of the SimpleDateFormat class from the java.text package. The SimpleDateFormat class provides a convenient way to convert a Date object to a String based on a specified format.