How-to articles, tricks, and solutions about JAVA

string to string array conversion in java

There are several ways to convert a string to a string array in Java. Here are a few options:

String.equals versus ==

In Java, the == operator is used to compare the primitive values of two variables, while the equals() method is used to compare the contents of two objects.

StringBuilder vs String concatenation in toString() in Java

In Java, you can use either a StringBuilder or string concatenation to create a string representation of an object.

Strip Leading and Trailing Spaces From Java String

To remove leading and trailing whitespace from a string in Java, you can use the trim method of the String class.

super() in Java

In Java, the super keyword is used to refer to the superclass of the current class. It is often used to call the constructor of the superclass, or to access methods or fields of the superclass that have been overridden in the current class.

Switch on Enum in Java

To use a switch statement on an enumeration (enum) in Java, you can use the enum type as the control expression for the switch statement.

Take a char input from the Scanner

To take a character input from the Scanner in Java, you can use the next() method to read a string and then use the charAt(int index) method to get the first character of that string. Here's an example:

Terminating a Java Program

There are several ways to terminate a Java program, depending on the context in which the program is running.

Testing Private method using mockito

It is generally considered bad practice to test private methods, as they are an implementation detail that should not be exposed to the outside world.

The import javax.servlet can't be resolved

If you are getting the error "The import javax.servlet can't be resolved", it means that the javax.servlet package is not available on the classpath.

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

This error typically occurs when you are trying to use the HttpServlet class in a Java project, but the necessary libraries are not included in the project's classpath. To fix this error, you need to add the servlet-api.jar library to your project's class

Tomcat Server Error - Port 8080 already in use

If you see the error "Port 8080 already in use" when starting the Tomcat server, it means that another process is already using port 8080 on your machine.

Tomcat: How to find out running Tomcat version?

There are several ways to find out the version of Tomcat that is running on your system:

Trusting all certificates using HttpClient over HTTPS

To trust all certificates when using the Apache HttpClient library to make HTTPS requests, you can create a custom X509TrustManager implementation that trusts all certificates and use it to create an SSLContext with a custom TrustStrategy.

Type List vs type ArrayList in Java

In Java, List is an interface that defines a list data structure, while ArrayList is a class that implements the List interface.

Unable to find valid certification path to requested target - error even after cert imported

If you are getting the "unable to find valid certification path to requested target" error even after importing the certificate, there are a few possible causes and solutions:<br>

Understanding Spring @Autowired usage

The @Autowired annotation in Spring is used to inject dependencies into a Java object.

Unfortunately MyApp has stopped. How can I solve this?

If you are seeing the error "Unfortunately, MyApp has stopped," it means that your Android app has crashed. This can be caused by a number of factors, including:

UnsatisfiedDependencyException: Error creating bean with name

UnsatisfiedDependencyException is a runtime exception that is thrown when the Spring framework is unable to resolve a dependency for a bean.

Use Mockito to mock some methods but not others

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

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.

Using Enum values as String literals

To use the values of an enum as string literals in Java, you can use the name() method of the Enum class. This method returns the name of the enum value as a string.

Using env variable in Spring Boot's application.properties

You can use environment variables in your application.properties file by using the spring.profiles.active property.

Using Pairs or 2-tuples in Java

In Java, you can use the javafx.util.Pair class to represent a pair or 2-tuple.

Using scanner.nextLine()

Scanner.nextLine() is a method of the Scanner class in Java that reads a line of text from the input.