Find the number which occurs odd number of time. All numbers occur even number of times except one.

import java.util.HashMap;
import java.util.Map;

public class Test {

 public static void main(String[] args) {

  //integer array declared with values
  int array[] = { 20, 40, 50, 40, 50, 20, 30, 30, 50, 20, 40, 40, 20};

  //declared map
  Map<Integer, Integer> map = new HashMap<>();

  //iterating int array to get the frequency of numbers
  for (int i = 0; i < array.length; i++) {
   if (map.containsKey(array[i])) {
    Integer value = (Integer) map.get(array[i]);

    map.put(array[i], ++value);
   } else {
    int key = array[i];

    map.put(key, 1);
   }
  }
  //iterating map to get the number which is present odd times
  map.forEach((key, value) -> {
      if(value % 2 != 0) {
       System.out.print("Number which occurs odd number of times is : 
       "+key);
      }
  });
 }
}

Share:

How to parse data from String in java.


package com.problem;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
public class Solution {
public static void main(String[] args) {
String str = "234$$##Shivaji$$##22.00";
String patternStr = "[^A-Za-z0-9.]";
Pattern pattern = Pattern.compile(patternStr);
String[] s = pattern.split(str);
String name = null;
Double doub = null;
Integer in = null;
for (String ss : s) {
if (StringUtils.isAlpha(ss)) {
name = ss;
} else if (StringUtils.isBlank(ss)) {
} else if (StringUtils.isAlphanumeric(ss)) {
in = Integer.valueOf(ss);
} else {
doub = Double.valueOf(ss);
}
}
System.out.println(name);
System.out.println(doub);
System.out.println(in);
}
}
Share:

Java program to find the occurence of integer in a given integer array.


package com.javatechtube;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IntegerOccurence {

  public static void main(String[] args) {

 int[] arr = {1,2,3,2,4,5};

 List<Integer> list = new ArrayList<>();

 Map<Integer, Integer> map = new HashMap<>();

 for(int i=0; i<arr.length;i++)
        {
    if(map.containsKey(arr[i]))
           {
  Integer value = (Integer)map.get(arr[i]);

  map.put(arr[i],++value);
    }
           else
           {
                int key = arr[i];

  map.put(key, 1);

  list.add(arr[i]);
    }
  }
         System.out.println(map);
     }
}
Share:

Java program to add all numbers present in AlphaNumeric String


First Approach
package com.nt.logical; import java.util.Scanner; public class AddNoFromString { public static void main(String[] args) { //getting the dynamic values from keyboard Scanner sc=new Scanner(System.in); System.out.println("Enter the String with number: "); String str=sc.nextLine(); //logic for removing the alpha characters String[] s=str.split("[A-Za-z]"); String ss=""; //logic to remove white spaces for(int i=0;i<s.length;i++) { ss=ss+String.valueOf(s[i]); } //getting number from ss obj int num=Integer.valueOf(ss); int sum=0; //logic to add the numbers while(num>0) { sum=sum+num%10; num=num/10; } //printing the value after adding System.out.println("Value :"+sum); } }
Second Approach
package com.nt.logical; import java.util.Scanner; public class AddNoFromString { public static void main(String[] args) { //getting the dynamic values from keyboard Scanner sc=new Scanner(System.in); System.out.println("Enter the String with number: "); String str=sc.nextLine(); //logic for removing the characters String[] s=str.split("[A-Za-z]"); int sum=0;
//logic to add the numbers for(int i=0;i<s.length;i++) { if(!(sArr[i].equals("")||(sArr[i].isBlank()))) { sum=sum+Integer.parseInt(s[i]); } } //printing the value after adding System.out.println("Value :"+sum); } }
Share:

Java program to create a custom Collection(ArrayList)


MyList.java(Interface)

package com.nt.util; public interface MyList { public void add(Object obj); }

MyArrayList.java(Class)

package com.nt.util; public class MyArrayList implements MyList{ private Object[] objArray; private static int elementCount=0; //default constructor public MyArrayList() { //initial size this.objArray=new Object[4]; } //Logic to add elements public void add(Object obj) { //checks if capacity is == size if(this.capacity()==this.size()) { //then it will create a new array Object[] tempArray=new Object[this.capacity()*2]; int i=0; //it will copy all the element to new array for(;i<(this.size());i++) { tempArray[i]=this.objArray[i]; } //it will insert the last element tempArray[elementCount++]=obj; //it will copy tempArray to objArray this.objArray=tempArray; } else { this.objArray[elementCount++]=obj; } } //returns the capacity private int capacity() { return this.objArray.length; } //returns the size private int size() { return elementCount; } @Override public String toString() { String str=java.util.Arrays.toString(this.objArray); return str.replaceAll("null",""); } }

ArrayListTest.java

package com.nt.test; import com.nt.util.MyArrayList; import com.nt.util.MyList; public class ArraylistTest { public static void main(String[] args) { MyList list=new MyArrayList(); list.add(23); list.add(45); list.add(43); list.add(3); list.add(55); System.out.println(list); } }
Share:

Java program to create upto 3 object for a class and throw Userdefined Exception if trying to create 4th one


ObjectCreation.java

package com.nt.object; public class ObjectCreation{ private static int count=0; private ObjectCreation() { count++; } public static ObjectCreation getInstance() { if(count<3) return new ObjectCreation(); else throw new ObjectCreationOutOfBoundException("Can't create more than three objects!!!"); } }

ObjectCreationOutOfBoundException.java

package com.nt.object; public class ObjectCreationOutOfBoundException extends RuntimeException { public ObjectCreationOutOfBoundException(String s) { super(s); } }

Test.java

package com.nt.object; public class Test { public static void main(String[] args) { ObjectCreation obj1=ObjectCreation.getInstance(); System.out.println("Created first object : "+obj1); ObjectCreation obj2=ObjectCreation.getInstance(); System.out.println("Created second object : "+obj2); ObjectCreation obj3=ObjectCreation.getInstance(); System.out.println("Created third object : "+obj3); //here exception will generate ObjectCreation obj4=ObjectCreation.getInstance(); } }
Share:

Generate Alpha Numeric String for the given size


import java.util.Random; import java.util.Scanner; public class GenerateAlphaNumericStringUsingGivenNo { public static void main(String[] args) { Scanner sc=null; int size=0; System.out.println("Enter the size"); sc=new Scanner(System.in); //taking the size from the keyboard size=sc.nextInt(); //calling the method to generate AlphaNumericString System.out.println(generate(size)); } //method for generating AlphaNumericString public static String generate(int size) { Random rand=null; StringBuffer sb=null; String numAndChars=null; //all string which will be choosen to generate numAndChars="abcdefghijklmnopqrstuvwxyz" +"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +"0123456789"; rand=new Random(); sb=new StringBuffer(""); for(int i=0;i<(size);i++) { //logic to generate AlphaNumeric String int index=(int)(numAndChars.length()*Math.random()); //appending the characters one by one sb.append(numAndChars.charAt(index)); } //returning the AlphaNumeric String return sb.toString(); } }
Share:

Quote of the day

Popular Posts

Featured Post

Collection Framework Overview

Collection:- A collection (sometimes called a container) is simply an object that groups multiple elements into single unit. Collect...

Youtube Page

Facebook Page

Recent Posts

About

I am Shivaji Chandra and I'm a Computer Science Engineer. I love to write poems, jokes and quotes. click here →