Category Archives: Uncategorized

Generate random number between 1 to 5 in Java

How to generate random number between 1 to 5 in Java: We can use nextInt() method of Random class to generate random number between 1 to 5 in java.

Output:

Note: If we just use nextInt(5), then it will give a random number between 0 to 4. So if you don’t want 0… Read More »

Generate random number between 1 to 10 in Java

How to generate random number between 1 to 10 in Java: We can use nextInt() method of Random class to generate random number between 1 to 10 in java.

Output:

Note: If we just use nextInt(10), then it will give a random number between 0 to 9. So if you don’t want 0… Read More »

Swap two numbers without using third variable Java

Swap two numbers without using third variable Java:

Output: Before swapping: x= 111 y= 222 After swapping : x= 222 y= 111   Recommended Posts: Covert String to int in Java Star pattern programs in Java Get last 4 chars from String Timeout test in TestNG How to clear an arraylist in java Java… Read More »

Capitalize first letter of each word in java

Capitalize first letter of each word in Java: 1. Split the string using space and assign it to an string array. 2. Loop through each string 3. Get the first letter of each string using substring method and capitalize it. 4. Get the remaining letters using another substring method which starts at index position 1.… Read More »

How to capitalize first letter of String Java

In this article, you will learn how to capitalize first letter of string java.  Here we have 2 approaches. 1. Use capitalize method of StringUtils 2. Using substring method   1. StringUtils.capitalize() method: This method is coming from apache. It capitalizes the first letter of a string. It will not capitalize any other letter. Examples:… Read More »

StringUtils capitalize

StringUtils capitalize(): This method capitalizes the first character of the string. No other characters are changed. Returns: the capitalized String, null if null String input Examples:

  Recommended Posts: Covert String to int in Java Star pattern programs in Java Get last 4 chars from String Timeout test in TestNG How to clear an… Read More »

Difference between flush and close in java filewriter

What is the difference between flush and close in java filewriter: flush() writes the contents of the buffer to the destination and makes the buffer empty for further data to store but it does not close the stream permanently. This means we can still write some more data to the stream. Whereas close() closes the… Read More »

Convert String to boolean in java

Convert String to boolean in java: In this tutorial, you are going to learn how to convert string to boolean in java. We can achieve it in two ways. 1 Boolean.parseBoolean() method 2 Boolean.valueOf() method Both the methods are null safe. It means if you pass a value other than true or false, it won’t throw null… Read More »

Python

Python is a popular programming language. It is used for software development. Python is used to handle big data and to perform complex operations. Python works on different platforms  like windows, mac, linux, raspberry pi. Python code is readable and it is similar to combination of english and mathematics. While loop in python:

Output:… Read More »

Arrays toString method in java

Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.The value returned by this method is equal to the value that would be returned by Arrays.asList(a).toString(),… Read More »

Arrays sort in java

Arrays.sort() method in java is used to sort the elements in ascending or descending order. Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2)… Read More »

Arrays.equals() in java

Arrays.equals() method returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements… Read More »

Arrays deepEquals in java

Arrays deepEquals in java: It returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number… Read More »

binary search in java

Searches the specified array for the specified object using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements (as by the sort(Object[]) method) prior to making this call. If it is not sorted, the results are undefined. (If the array contains elements that are… Read More »

Arrays in Java

Arrays class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted. The documentation for the methods contained in this class… Read More »

Find the third largest number in an array in Java

Find the third largest number in an array in Java: In this article, learn how to find the third largest number in an array in java.

Answer: 40 Explanation:  1. Sort the numbers in the array by comparing the adjacent numbers. Means if an array is having three numbers i.e 2,1,3, then  compare 2… Read More »

random string java

In this tutorial, you are going to learn how to generate a random string in java. We are using randomAlphabetic method present in RandomStringUtils class of Apache to generate a random string java. apache.commons.lang3.RandomStringUtils.randomAlphabetic: Creates a random string whose length is the number of characters specified. Characters will be chosen from the set of Latin… Read More »

StringUtils isAlphanumeric

StringUtils isAlphanumeric checks if the CharSequence contains only Unicode letters or digits. null will return false. An empty CharSequence (length()=0) will return false. Examples: StringUtils.isAlphanumeric (null) = false StringUtils.isAlphanumeric (“”) = false StringUtils.isAlphanumeric (” “) = false StringUtils.isAlphanumeric (“abc”) = true StringUtils.isAlphanumeric (“ab c”) = false StringUtils.isAlphanumeric (“ab2c”) = true StringUtils.isAlphanumeric (“ab-c”) = false Parameters:… Read More »

StringUtils getDigits method

StringUtils getDigits method checks if a String str contains Unicode digits, if yes then concatenate all the digits in str and return it as a String. An empty (“”) String will be returned if no digits found in str. Examples: StringUtils.getDigits (null) = null StringUtils.getDigits (“”) = “” StringUtils.getDigits (“abc”) = “” StringUtils.getDigits (“1000$”) =… Read More »

StringUtils lowercase

StringUtils lowercase method converts a string to lowercase. Examples:

Note: As described in the documentation for String.toLowerCase(), the result of this method is affected by the current locale. For platform-independent case transformations, the method lowerCase(String, Locale) should be used with a specific locale (e.g. Locale.ENGLISH). Summary: From this method we have learned how to… Read More »

StringUtils indexOf

StringUtils indexOf method returns the index of the first occurrence of the specified character from a string. If it doesn’t find the character then it returns -1. A null CharSequence will return -1. Syntax: StringUtils.indexOf (CharSequence seq, CharSequence searchSeq); Parameters: seq the CharSequence to check, may be null searchSeq the CharSequence to find, may be… Read More »

StringUtils upperCase

StringUtils upperCase method converts a string to uppercase. Parameters: str the String to upper case, may be null Returns: the upper cased String, null if null String input StringUtils uppercase Examples:

Below are the methods provided in StringUtils class and these are all null safe. Equals/Compare: It compares whether two given strings are equal or not. EqualsAny: Compares… Read More »

StringUtils endsWith

StringUtils endsWith method checks if a string ends with a specified character/suffix and return true otherwise returns false. nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive. Examples:

  Below are the methods provided in StringUtils class and these are all null safe. Equals/Compare: It compares whether two… Read More »