Posts

6 ways to Concatenate Strings in Java (Bonus: Choose concatenation character)

Image
We need to deal with Strings in real time projects, and concatenation of Strings is a common and important functionality. There are multiple ways supported in Java to concatenate 2 or more Strings. Let's try to understand what are our options: 1. Using the '+' operator This is the easiest and the most commonly used option. We can simply use '+' between 2 Strings to concatenate them. String s1 = "Hello" ; String s2 = "World" ; System . out . println ( s1 + s2 ) ; Output: HelloWorld 2. Using concat() function Java has inbuilt function for concatenation. + operator is used for addition operations as well. If you want your code to be specific and readable, concat() function is an easy approach. String s1 = "Hello" ; String s2 = "World" ; System . out . println ( s1 . concat ( s2 ) ) ; Output: HelloWorld 3. Using StringBuilder class StringBuilder class has inbuilt methond append(), which we can use for concatenati...

4 ways to compare Strings in Java (Bonus: Lexicographical Compare)

Image
  Comparing two Strings is a very common project requirement. It can be utilized in multiple ways.  The comparison can be done to verify that the output is as expected.  It can be utilized to validate a test case. There can be a functionality which depends on a certain input, and we can write conditions on the basis of input given. We have to compare in order to verify the correct input and direct to correct condition. So, we can say that String comparison is a very important functionality in Java.  Today we'll learn multiple ways to do String comparison. 1. Using Inbuilt function equals() The java documentation says : boolean java.lang.String.equals(Object anObject)  Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Let's check out an example: String s1 = "Newstring" ; String s2 = "NewString" ; String s3 = new...

4 ways to add New Line Character to a String in Java

Image
When you are required to output Strings with your Java code, more often than not, there is a requirement to format the output correctly.  Adding a newline character between two strings in order to separate them, or create a paragraph is one of the most common requirements. Let's look at a few ways today, on how to output a newline between two Strings. 1. System.out.println inbuilt function One of the easiest ways, is to use the function System.out.println(). The println() function will automatically add a new line to the output, if using two strings.  Sample: String s1 = "This is a String." ; String s2 = "This is another String." ; System . out . println ( s1 ) ; System . out . println ( s2 ) ; If you don't want to use a separate System.out.println for every String, there are other ways as well. Read on! 2. Add a newline character "\n" You can simply append newline character yourself to the strings, and use a single System.out.print command...

Find Day of the week from any Calendar date using Java code

Image
  If we have a date, and need to find which day of the week it was instantly, that would a really useful function, isn't it? This is also a useful function which can be used on a webpage having Calendar logic, or a Delivery website which wants to show which is the day of the week on which your order will be delivered.  Java has a very useful inbuilt function which we can use to get this functionality, and we'll show you how! For utilizing the functionality, you have to import a Java library:  import java . time . LocalDate ; Once you have the library, the code is really easy. Let's write a small working function. /**   * Function to return Day of the week   * @author computengine.com   * @param month   * @param day   * @param year   * @return  */ public static String findDay(int month, int day, int year) { LocalDate ldt = LocalDate . of ( year , month , day ) ; return String . valueOf ( ldt . getDayOfWeek ...

Create a File logging Utility using Java Code

Image
  Many times during coding, we need to log our outputs or comments. Usual way to do that is to print the log on the Console using System.out.println() function.  However, there can be a requirement to print the output or comments to a file so that it can be referenced later. One option is to use log4j library which is a standard logging utility.  However, you can also create your own utility using a few lines of Java Code! Let's check out how.  We'll try to cover the following utilities which can form the base of a logging tool: 1. File logging by passing the value and the file path. 2. File logging by passing the value and setting the log path in a properties file.  3. Clear the log file.  Let's first take a look at a basic File logging utility.  1. File logging by passing the value and the file path. /**  * Writes data/logs to a file. We'll pass both the filePath where the log needs to be written, and the fileValue which needs to be written. ...

Find the file size using Java code (Bytes, KB, MB and GB)

Image
  There are multiple instances when you want to find out or display the size of a file. It can be for filtering out files of certain file sizes or limit the upload size of a file. For that purpose, we'll have to find out the file size.  We'll utilize an inbuilt function which returns the size in bytes, and we'll write our own functions to find out file sizes in KB, MB and GB i.e. KiloBytes, MegaBytes, and GigaBytes. The function that we'll be using is file.length(). Now for the implementation. If you want to display the file size, the String format is more suitable.  /**  * Get File size in GB  * @author computengine.com  * @param file  * @return  */ public static String getFileSizeGigaBytes ( File file ) { return Double . toString ( ( double ) file . length ( ) / ( 1024 * 1024 * 1024 ) ) ; } /**  * Get File size in MB  * @author computengine.com  * @param file  * @return  */ public stat...

Find the HCF of 2 numbers using Java code

Image
  Today we'll try to solve a common mathematical question using Java code. We will find the HCF of two numbers.  What is HCF - its the Highest Common Factor of two numbers. Which means it is the biggest number which divides two numbers such that the remainder is 0. What will be utilized in this logic: 1. While loop to iterate till we arrive at the solution 2. % Operator to find the remainder Let's check the code! /**  * Find the HCF of two numbers  * @author computengine.com  * @param num1  * @param num2  * @return  */ public static void findHCF ( int num1 , int num2 ) { int remainder = 0 ; System . out . println ( " Input Num1: " + num1 ) ; System . out . println ( " Input Num2: " + num2 ) ; //loop till we find a number which divides exactly while ( num1 % num2 > 0 ) { //find the remainder of the division remainder = num1 % num2 ; System . out . println ( " Remainder: " + remainder ) ; //pu...