You can check for the presence of vowels(a, e, i, o, u) in a string and list them using a java program. There are following two ways in which this can be done. Method 1: Iteration over the string Follow the below steps to write a program to check the vowels in a string using iteration. Initialize a string with all the vowels. Iterate over the characters of the string in which you want to check the vowels. In very iteration, check if the current character is from among the characters of string in Step 1(all vowels). If it is, then it is a vowelRead More →

Suppose you want to know how many objects of a class have been created during a program execution. How would you do that? Using constructor When an object of a class is created, its constructor is always called. Thus, if we keep track of how many times the constructor of a class is called, we directly know number of its objects that have been created. Thus, we need to add some counter in a constructor and increment its value by 1 in every invocation. This counter should be a class level variable. Since class level variables have different values for each object, we have toRead More →

Generation of random sequence of characters is often required when you want to display a unique transaction id or as a random password generator which provides a temporary password for user registering for the first time on a website or while creating captcha for preventing automated input etc. Java provides many different ways to program a random string generator application and this post will explain most of those. Method 1: Using UUID java.util.UUID class can be used to generate a random string. Its static randomUUID method acts as a random alphanumeric generator and returns a String of 32 characters. If you want a string ofRead More →

Determining the number of words in a file is an important aspect of programming. It is required while solving programming problems and it is also utilized in real world applications. This post shall detail out different methods to count the number of words in a file. All of the methods involve the logic to read a file, thus you should know how to read a text file in java. This post will definitely help you out on that. Method 1: Using BufferedReader Read a file line by line using java.io.BuffereredReader. Break this line into words by splitting over it using split method of java.lang.String. ThisRead More →

What is sorting? Sorting means to arrange elements of a collection in an order which can be either ascending or descending. Collection can be an array, a list, keys or values of a map etc. Usually when sorting techniques are discussed, the collection is sorted in ascending order of its elements and the collection picked up is an array. There are many approaches available to sort an array. This post will focus on Bubble sort. What is Bubble sort? It is a sorting algorithm in which each array element is compared with the element next to it. If the element being compared is greater thanRead More →

Swapping strings Swapping two strings means interchanging their values with one another. Example, there are two string variables s1 and s2 with values s1 = “codippa.com” and s2 = “the website”, Swapping the two strings will make their value as s1 = “the website” and s2 = “codippa.com” Common Approach The most common approach of swapping two strings is to store the value of one of the strings into a temporary variable and then assigning the value of second string to first one. Finally, assigning the value of temporary variable back to the first string as shown below String s1 = “codippa.com”; String s2 =Read More →

What is a prime factor A prime factor is made up of two terms : Factor and Prime Number. Prime Factor  = Factor + Prime Number A factor of a number is a number which completely divides the number. By completely divides, it means that the remainder of division is zero. A prime number is a number which is only divisible by itself and 1. Examples of prime numbers are 2, 3, 5, 7, 11, 17, 29 etc. Logic Prime factors of a number can be calculated using the following steps: Iterate over a loop using a counter starting from 2(since it is the smallestRead More →

What is Fibonacci Series Fibonacci Series is a sequence of numbers in which each number(starting from the third number) is a sum of previous two numbers in the series and the first 2 elements of the series are 0 and 1. Thus the series becomes, 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on…. Problem Given a number as input, determine whether it belongs to the Fibonacci series or not. Example, input numbers such as 0, 5, 34, 55 belong to Fibonacci series but numbers such as 4, 9, 20, 52 do not belong to it. We need to write a javaRead More →

Problem Suppose there are 2 strings and their values need to be interchanged. String variables are declared as String stringOne = “This is “; String stringTwo = “codippa.com “; and the requirement is to interchange(or swap) their values such that their values become stringOne = “codippa.com “; stringTwo = “This is “; A typical approach to interchange the values of these string variables is by using a third variable as String temp = “”; // store value of first string in temp temp = stringOne; // store the value of second string into first string stringOne = stringTwo; // store the value stored in tempRead More →