Posts

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 static String getFileSizeMegaBytes ( File file ) {

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 ) ; //put

Find the Longest Streak of 1's in a Binary Array using Java Code

Image
  You have been given an Integer Array which contains only 1s and 0s, and asked to find the longest running streak of 0 or 1 in the array. Sound familiar? Its a common interview question. Let's try to find a solution using Java.  We would required two counters here, 1 counter to keep track of the running count, and the second one to keep track of the maximum running streak found.  We'll loop through the array using a for loop.  Here is the code for your reference: /**  * Find the longest Streak of 1s  * @author computengine.com  * @param a  * @return the length of the longest streak  */ public int findLongestStreak ( int [ ] b ) { System . out . println ( " Array for processing: " + Arrays . toString ( b ) ) ; int count = 0 , max = 0 ; //loop through the array for ( int i = 0 ; i < b . length ; i + + ) { //increment count when 1 is found if ( b [ i ] = = 1 ) count + = 1 ; //if count is greater than previous max streak,

JavaScript Input Field Validation Function with Input field color highlight and Error Message Display

Image
  We use a lot of Forms regularly. If you are using a Form in your website, have you ever tried to create some validations on the form so that the inputs made by user can be checked? There are a lot of libraries out there which can help you do the input validation using JavaScript, but that is additional code that needs to be included, and third party libraries can often slow down your page load speeds.  Today, we'll write a small JavaScript function that we can include in our website and which does the input validation very quickly and efficiently. This function also highlights the input field in a color of your choice, and you can display an error message to your end user. If you have a form with multiple fields, and want to run validations on all of them, you'll have to loop through the fields one by one using a for loop, and run the validation on each one.  You can pass the field ID to the function and run the validation on it.  Below is our function which will do the valid

Java Program to Read a Properties file and return a Property Value

Image
  Properties file in Java is used to store configurations, data or settings for a project. They have been utilized as Object Repositories for Automation Projects in the past (and still are used in legacy projects) until the Page Object Model came along.  This makes reading of Properties files an important concept in Java. This Tutorial will cover the below topics: 1. Create  Properties file in a Java Project 2. Set Properties in the file 3. Read the Properties file using Java code and return the value Let's first see how to create a Properties file for your Project. Its really easy.  In Eclipse, right click on the folder where you want to create the file, and navigate to New -> File. Now enter the file name, and make sure to enter the extension as .properties The file has been created! Now, the next step, i.e. enter some data in the file that we'll read using Java. The properties stored in the properties file have to follow a certain format.  You can enter like below: write

What is my IP Address - How to find your IP address on Local Network using Command Prompt, and External Network using JavaScript Code

Image
  IP Address or Internet Protocol Address, is a set of numeric (IPv4) or Alphanumeric (IPv6) characters, which uniquely identify a system on the internet. As per wikipedia: An Internet Protocol address is a numerical label such as 172.16.254.1 that is connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: host or network interface identification and location addressing.   Wikipedia So, the next question is, what is my IP address? Now, there are two kinds of IP address, Internal IP - which identifies your machine on an internal network such as your home network connected to a router, and External IP, which identifies the network on the WWW i.e. the world wide web.  Let's try to find our IP address! To find your Internal IP address, you just need to know a few basic commands.  Open the Command Prompt on a Windows machine, press Windows + R: Click on OK. Now, simply type the command - ipconfig This simple command wil

How to create a SQLite Database for your Project and why it is the Perfect Choice for a $5 VPS Hosting

Image
  If you are looking to create a Database of your own for your own side project, or a hobby project on the World Wide Web, there are many options available. The really common ones are using a MySQL database, or some Tech Gurus will tell you to use Cloud based databases. Now, technically all that is fine, but in reality that costs money! $$ is the thing that you need to pull out of your pockets if you want a decent database up and running for your project. Why? Because, a decent database requires an installation and a fair amount of RAM needs to be allocated to it in order for it to be running successfully. Now, if your project is a small or hobby project, there are fair chances that you are using a cost effective hosting solution for it. The reason that these hosting solutions are cost effective is, they are not very high on power.  Again, if you are using a $5 VPS (i.e. the lowest end Virtual Machine available or most hosting providers) on a Digital Ocean, Google Cloud, Azure or AWS p