Monthly Archives: June 2020

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 »