This page contains Java code for below questions:
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
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
No comments:
Post a Comment