Menu

regex to match only number with n digit



^[0-9]*$



import java.util.regex.Matcher; import java.util.regex.Pattern; final String regex = "^[0-9]*$"; final String string = "12333\n" + "sgd123\n" + "sefgwer46546\n" + "486464se\n" + "486464545656"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println( System.out.println( System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println( System.out.println( System.out.println("Group " + i + ": " + matcher.group(i)); } } 

How to swap two String variables without using the third variable?

We are going to see how to swap two variable of type String without using any third or temporary variable. 
This is very frequent question asked by interviewer in any Java technical interview.

Below is a simple java program to swap two String variables a and b.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package jorvee;

public class SwapTwoStringVariableWithoutUsingTheThirdVariable {

 public static void main(String[] args) {
  String a="swap", b="two numbers";
  a= a+b;
  b= a.substring(0, a.length() - b.length());
  a= a.substring(b.length());
  System.out.println("After swaping value of a is: "+a);
  System.out.println("After swaping value of b is: "+b);

 }

}

Output:

After swaping value of a is: two numbers
After swaping value of b is: swap

How to calculate interest on Saving account?

Formula to calculate the interest on Saving account: 
Amount * ROI / 100 * No Of Days / 365

Here Amount is the base amount available in your account.
ROI: Rate of interest which your bank is providing you on your saving account.  
No of days: Total number of days that amount remain in your saving account.

lets take an example:
I have a saving account in state bank of India. State bank gives 4% rate of interest annually on saving accounts and I kept rupees 100,000 in my saving account for 15 days. Then how much interest I will earn from this.

Amount: 100000
ROI: 4
No of Days: 15 
lets calculate using the formula:
Total interest = Amount * ROI / 100 * No Of Days / 365
= 100000 * 4/100 * 15/365
 = 164.38
Total interest a account holder will earn 164.38 rupees in 15 days on the amount of rupees 100000.