Important Java Selenium Questions/Codes for Software Test Automation Learners, Part3
This page contains Java code for below questions:
1. Reverse characters
2. Create Palindrome
3. Find unique characters(Remove duplicate characters)
4. Count the repetition of characters(find duplicate characters) in a given string
5. Create Fibonacci sequence/number
6. Regular expression
Find the sum of all the numbers present in a given string containing alphanumeric and special characters.
7. Sum all the characters present in your name
8. Find non-duplicate characters in a given string.
9. Check if the given number is a "prime number"
10. Odd numbers present in an array
11. Find the second most repeated char in a given string
12. Reformat the given string : "asdfsdafgde" => "2a-2s-3d-1e-2f-1g"
1. Reverse
characters
public static void reverseChar() {
String str = "asdfghjkl";
//lkjhgfdsa
String expStr = "";
int len = str.length();
for(int i=len-1;i>=0;i--) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
expStr = expStr+strCh;
}
System.out.println("The reverse string is : "+expStr);
}
OUTPUT:2. Create
Palindrome
public static void palindrome() {
String str = "level";
//lkjhgfdsa
String expStr = "";
int len = str.length();
for(int i=len-1;i>=0;i--) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
expStr = expStr+strCh;
}
//System.out.println("The
reverse string is : "+expStr);
String strInSameOrder = "";
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
strInSameOrder = strInSameOrder+strCh;
}
//System.out.println("The
string in right/same order is : "+strInSameOrder);
if(expStr.equals(strInSameOrder)) {
System.out.println("The given string is a Palindrome.");
} else {
System.out.println("The given string is NOT a Palindrome.");
}
}
OUTPUT:
The given string is a Palindrome.
3. Find
unique characters(Remove duplicate characters)
public static void uniqueChar() {
String str = "asdfsgdfsdasderfvdera";
ArrayList<String> alist = new ArrayList<>();
char ch = str.charAt(0);
String strCh = Character.toString(ch);
alist.add(strCh);
int len = str.length();
for(int i=0;i<len;i++) {
char chh = str.charAt(i);
String strChh = Character.toString(chh);
if(!alist.contains(strChh)) {
alist.add(strChh);
}
}
System.out.println("The unique characters are : "+alist);
}
OUTPUT:
The unique characters are :
[a, s, d, f, g, e, r, v]
4. Count
the repetition of characters(find duplicate characters) in a given string
public static void duplicateChar() {
String str = "asdfsgdfsdasderfvdera";
int len = str.length();
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
if(map.containsKey(strCh)) {
map.put(strCh, map.get(strCh)+1);
} else {
map.put(strCh, 1);
}
}
System.out.println("The duplicate chars are : "+map);
}
OUTPUT:
The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=3, v=1, g=1}
5. Create
Fibonacci sequence/number
public static void fibonacciSeq() {
//
0,1,1,2,3,5,8,13,....
ArrayList<Integer> alist = new ArrayList<>();
alist.add(0);
alist.add(1);
for(int i=2;i<10;i++) {
alist.add(alist.get(i-1)+alist.get(i-2));
}
System.out.println("The Fibonacci sequence is :"+alist);
}
OUTPUT:
The
Fibonacci sequence is :[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
6. Regular expression
Find the sum of all the numbers present in a given string containing alphanumeric and special characters.
public static void reqExpressionSum() {
String str = "as3df5cs@11we4zas#21$juyt";
String strArr = str.replaceAll("[^0-9]", " ");
System.out.println("strArr = "+strArr);
String[] strSum = strArr.split(" ");
System.out.println("Array of numbers = "+Arrays.toString(strSum));
int len = strSum.length;
int sum = 0;
for(int i=0;i<len;i++) {
if(!strSum[i].equals("")) {
int x = Integer.parseInt(strSum[i]);
sum = sum+x;
}
}
System.out.println("Sum of the numbers present in the string = "+sum);
}
OUTPUT:
strArr
= 3
5 11 4
21
Array of
numbers = [, , 3, , 5, , , 11, , 4, , , , 21]
Sum of the numbers present in the string = 44
7. Sum all the characters present in your name
public static void sumName() {
String name = "John Smith";
int nameSum = 0;
for(int i=0;i<name.length();i++) {
char nthchar = name.charAt(i);
//System.out.println("nthchar = "+nthchar);
int j = nthchar;
System.out.println("nthchar, j = "+nthchar+" "+j);
nameSum = nameSum+nthchar;
}
System.out.println("Summation of all characters in the name = "+nameSum);
}
OUTPUT:
nthchar, j = J 74
nthchar, j = o 111
nthchar, j = h 104
nthchar, j = n 110
nthchar, j = 32
nthchar, j = S 83
nthchar, j = m 109
nthchar, j = i 105
nthchar, j = t 116
nthchar, j = h 104
Summation of all characters in the name = 948
8. Find the non duplicate chars in a given string
public static void nonDuplicateChar() {
String str = "asdfsgdfsdasdferfvdera";
int len = str.length();
HashMap<String,Integer> map = new HashMap<>();
//String chStr = "";
for(int i=0;i<len;i++) {
String str1= Character.toString(str.charAt(i));
if(map.containsKey(str1)) {
map.put(str1, map.get(str1)+1);
} else {
map.put(str1, 1);
}
}
System.out.println("The char repeation is like this "+map);
ArrayList<Integer> alist = new ArrayList<>(map.values());
ArrayList<String> blist = new ArrayList<>(map.keySet());
for(int i=0;i<alist.size();i++) {
if(alist.get(i)==1) {
System.out.println("The unique numebr ="+alist.get(i)+blist.get(i)); //OR user concat like below
//System.out.println("The unique numebr ="+Integer.toString(alist.get(i)).concat(blist.get(i)));
}
}
}
Output:
The char repeation is like this {a=3, r=2, s=4, d=5, e=2, f=4, v=1, g=1}
The unique numebr =1v
The unique numebr =1g
9. Find if the given number is a Prime number such as 2,3,5,7,11,13,17
public static void primenum() {
int x = 9;
boolean isPrimeNumber = true;
if(x<=1) {
isPrimeNumber=false;
}
for(int i=2;i<x;i++) {
if((x % i == 0)) {
isPrimeNumber = false ;
break ;
}
}
System.out.println("is the given number prime ? "+isPrimeNumber);
}
Output:
When x=2:
is the given number prime ? true
when x=9;
is the given number prime ? false
10. odd numbers present in an array
public static void oddNum() {
int[] num = {1,2,4,5,7,6,8,3,0,10,11,15,16};
for(int i=0;i<num.length;i++) {
if(!(num[i]%2==0)) {
System.out.println("Number at index "+i+" is an odd number "+num[i]);
}
}
}
Output:
Number at index 0 is an odd number 1
Number at index 3 is an odd number 5
Number at index 4 is an odd number 7
Number at index 7 is an odd number 3
Number at index 10 is an odd number 11
Number at index 11 is an odd number 15
11. find the second most repeated char in a given string
public void secMostRepChar() {
String str = "asdfsgdfsdasdferfvdera";
int len = str.length();
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
if(map.containsKey(strCh)) {
map.put(strCh, map.get(strCh)+1);
} else {
map.put(strCh, 1);
}
}
System.out.println("The duplicate chars are : "+map);
int mapLen = map.size();
int map2ndVal = 0;
ArrayList<Integer> alist = new ArrayList<>(map.values());
ArrayList<String> blist = new ArrayList<>(map.keySet());
for(int i=1;i<mapLen;i++) {
if((alist.get(i-1))>map2ndVal && (alist.get(i-1)<alist.get(i))) {
map2ndVal=alist.get(i-1);
}
}
System.out.println("The second highest repeated number : "+map2ndVal);
ArrayList<String> clist = new ArrayList<>();
for(int i=0;i<alist.size(); i++) {
if(alist.get(i)==map2ndVal) {
//clist.add(blist.get(i)+Integer.toString(map2ndVal)); OR clist.add((blist.get(i)).concat(Integer.toString(map2ndVal)));
}
}
System.out.println("The second highest repeated char/s : "+clist);
}
Output:
The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=4, v=1, g=1}
The second highest repeated number : 4
The second highest repeated char/s : [s4, f4]
12. Reformat the given string
public static void reformatString() {
String str = "asdfsdafgde"; //2a-2s-3d-1e-2f-1g
int len = str.length();
HashMap<String,Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
String str1= Character.toString(str.charAt(i));
if(map.containsKey(str1)) {
map.put(str1, map.get(str1)+1);
} else {
map.put(str1, 1);
}
}
System.out.println("The char repeation is like this "+map);
ArrayList<String> alist = new ArrayList<>(map.keySet());
ArrayList<Integer> blist = new ArrayList<>(map.values());
ArrayList<String> clist = new ArrayList<>();
String strNewFormat = "";
for(int i=0;i<map.size();i++) {
String str1 = Integer.toString(blist.get(i))+alist.get(i);
clist.add(str1);
}
System.out.println("combined elements "+clist);
for(int i=0;i<map.size()-1;i++) {
//strNewFormat=strNewFormat+clist.get(i)+"-"; //OR
strNewFormat = strNewFormat.concat(clist.get(i)).concat("-");
}
strNewFormat=strNewFormat+clist.get(map.size()-1);
System.out.println("New formated string: "+strNewFormat);
}
Output:
The char repeation is like this {a=2, s=2, d=3, e=1, f=2, g=1}
combined elements [2a, 2s, 3d, 1e, 2f, 1g]
New formated string: 2a-2s-3d-1e-2f-1g