Apr 19

Converting String Array to Integer Array Example Java

Java Program to Convert String Array to Integer Array:
In this below program we have converted string array to integer array.

package in.javadomain;

public class StringArrayToIntArray {
	public static void main(String[] args) {
		String[] strArray = { "10", "20", "30", "40", "50" };
		int[] intArray = new int[strArray.length];
		for (int i = 0; i < strArray.length; i++) {
			intArray[i] = Integer.parseInt(strArray[i]);
		}

		for (int i = 0; i < intArray.length; i++) {
			System.out.println("Integer Array Values ::  " + intArray[i]);
		}
	}

}

Output:

Integer Array Values ::  10
Integer Array Values ::  20
Integer Array Values ::  30
Integer Array Values ::  40
Integer Array Values ::  50

Recommended Java Books:

Apr 15

Java Program to find two letter capital words using Regex

Java Program to find two letter capital words in Java using Regex:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexAppealAddress {
	public static void main(String[] args) {
		String input = "Javadomain is also called as JD which contains JAVA tutorials and programmings";
		// This prints JD and JA
		// Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}");
		// This prints only JD
		Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}\\s");
		Matcher matcher = regexPattern.matcher(input);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

Output:

 JD 

Explanation:

1. \\s for whitespaces
2. [A-Z] for only capital letters
3. {2,2} for minimum two letters and maximum two letters.

Java Recommended Books:

Apr 15

Arraylist vs LinkedList Java

ArrayList Vs LinkedList:

ArrayList:
1. Search operation is faster, because it maintains the array structure, so get(index) will search and give the value fastly.
2. Less memory consumption because it maintains element and data. Where in linkedlist it maintains two pointer to store the addresses of the neighbour elements.

LinkedList:
1. Remove operation is faster.
2. Insertion operation is faster.
Because it is doubly linked list, so maintains both the address, where as in arraylist it need to shift the deleted area with the neighbour values.

When to use ArrayList:
Arraylist is recommended if we use search operation more. (get(index) function).

When to use LinkedList:
LinkedList is recommended if we use add/remove operations more. (add() and remove() function).

Recommended Books:

Apr 13

Finding difference in java example:

Java program to find the difference:
Takes array of integer inputs and finding the difference

package in.javadomain;

public class FindDifference {
	public static void main(String[] args) {
		// inputs here
		int[] i = { 1,11,21,31 };
		int temp = i[1] - i[0];
		int k = 1;
		int lastValue = i[i.length - 1];
		boolean logicCheck = false;
		boolean wrongLogic = true;
		for (int j = 0; j < i.length; j++) {
			if (k < i.length) {
				if (i[j] == lastValue) {
					if (wrongLogic) {
						logicCheck = true;
					} else {
						logicCheck = false;
					}
				} else {
					if ((i[j + k] - i[j]) == temp) {
						logicCheck = true;
					} else {
						logicCheck = false;
						wrongLogic = false;
					}

				}

			}

		}
		if (logicCheck) {
			System.out.println("Difference is :::" + temp);
		} else {
			System.out.println("Not able to find, sorry!!");
		}
	}
}

Output:

Difference is :::10

Recommended Java Books:

Apr 13

Removing some values from the List in Java Example

Remove one List value from another list:

Java Program to Remove List Values from another List values:
In this program we are deleting the list2 values from list1.

package in.javadomain;

import java.util.ArrayList;
import java.util.List;

public class RemoveListValue {
	public static void main(String[] args) {
		List<String> myList1 = new ArrayList<String>();
		myList1.add("Java");
		myList1.add("PHP");
		myList1.add("C");
		List<String> myList2 = new ArrayList<String>();
		myList2.add("Java");
		myList2.add("PHP");

		System.out.println("List 1 Values before removing");
		for (String list1Val : myList1) {
			System.out.println(list1Val);
		}

		myList1.removeAll(myList2);

		System.out.println("List 1 Values after removing");
		for (String list1Val : myList1) {
			System.out.println(list1Val);
		}
	}

}

Output:

List 1 Values before removing
Java
PHP
C
List 1 Values after removing
C

Java Program to Remove Some values from List Example:
In this program we are searching “Java” from list1 and adding the value in list2, then using the removeAll function, deleting the list2 value from list1.

package in.javadomain;

import java.util.ArrayList;
import java.util.List;

public class RemoveListValue {
	public static void main(String[] args) {
		List<String> myList1 = new ArrayList<String>();
		myList1.add("Java");
		myList1.add("PHP");
		myList1.add("C");
		List<String> myList2 = new ArrayList<String>();

		for (String list1Value : myList1) {
			if (list1Value.equalsIgnoreCase("Java")) {
				myList2.add(list1Value);
			}
		}

		myList1.removeAll(myList2);

		System.out.println("List 1 Values after removing");
		for (String list1Val : myList1) {
			System.out.println(list1Val);
		}
	}

}

Output:

List 1 Values after removing
PHP
C

Recommended Java Books:

Apr 11

Java Regex Pattern to allow characters and spaces Example

Java Regex Pattern to allow characters and spaces Example:

Requirements:

 Java domain   - Should be allowed
               - Should Not allowed
     Java      - Should be allowed
  (Java)       - Should Not allowed
Javadomain     - Should be allowed

Java Regex Program to allow characters and whitespaces And also not allow only whitespaces:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexSpace {

	public static void main(String[] args) {
		String input = " Java domain  ";
		Pattern regex = Pattern
				.compile("^$|[~\\!\\@\\#\\$\\%\\^\\&\\.\\,\\*\\(\\)\\_\\+\\=\\{\\}\\|\\:\\\\'\\[\\]\\<\\>\\?\\/\\'\\^[0-9]]");
		Matcher matcher = regex.matcher(input.trim());
		if (matcher.find()) {
			System.out.println("Not Allowed");
		} else {
			System.out.println("Allowed");
		}
	}

}

Output:

Allowed

Recommended Java Books:

Apr 10

Finding Common values in Arraylist Java Example

Finding Common values in Arraylist:

Two arraylists are iterated one inside another for loop and common values are inserted in the common arraylist.

Input:

List 1 contains:

God
.Net
PHP
Java

List 2 contains:

C
Java
C++
.Net

Expected Values:

Common values are :::Java
Common values are :::.Net

Java Program to Find Common values in Arraylist:

import java.util.ArrayList;
import java.util.List;

public class FindCommonValue {
	public static void main(String[] args) {
		List<String> myList1 = new ArrayList<String>();
		myList1.add("God");
		myList1.add(".Net");
		myList1.add("PHP");
		myList1.add("Java");
		List<String> myList2 = new ArrayList<String>();
		myList2.add("C");
		myList2.add("Java");
		myList2.add("C++");
		myList2.add(".Net");

		List<String> commonList = new ArrayList<String>();

		for (String list2Val : myList2) {
			for (String list1Val : myList1) {
				if (list2Val.equalsIgnoreCase(list1Val)) {
					commonList.add(list2Val);
				}
			}
		}

		for (String string : commonList) {
			System.out.println("Common values are :::" + string);
		}
	}
}

Output:

Common values are :::Java
Common values are :::.Net

Recommended Java Books:

Apr 10

Compare Three List in Java Example

Arraylist/List comparison in Java:
Now we have a scenario to split three list/arraylist values to seven lists with respect to list/arraylist values.

Input:

List 1 contains:
Java
PHP
26
God

List 2 contains:
C
Java
PHP

List 3 contains:
26
Guru
C 
Java

Expected Output:

only in list 1 ::: God
only in list 2 ::: 
only in list 3 ::: Guru
In 1 and 2 ::: PHP
In 1 and 3 ::: 26
In 2 and 3 ::: C
common in all lists ::: Java

Three List Comparison Java Source code:

import java.util.ArrayList;
import java.util.List;



public class ListCompare {
public static void main(String[] args){
	List<String> myList1 = new ArrayList<String>();
	myList1.add("Java");
	myList1.add("PHP");
	myList1.add("26");
	myList1.add("God");
	List<String> myList2 = new ArrayList<String>();
	myList2.add("C");
	myList2.add("Java");
	myList2.add("PHP");
	List<String> myList3 = new ArrayList<String>();
	myList3.add("26");
	myList3.add("Guru");
	myList3.add("C");
	myList3.add("Java");
	
	List<String> commonList = new ArrayList<String>();
	List<String> commonInList1and2 = new ArrayList<String>();
	List<String> commonInList1and3 = new ArrayList<String>();
	List<String> commonInList2and3 = new ArrayList<String>();
	
	for(String list2Val : myList2){
		for(String list1Val : myList1){
			for (String list3Val : myList3) {
				if(list3Val.equalsIgnoreCase(list1Val) && list3Val.equalsIgnoreCase(list2Val)){
					commonList.add(list3Val);
				}
			}
			
		}
	}
	
	
	myList3.removeAll(commonList);
	myList2.removeAll(commonList);
	myList1.removeAll(commonList);
	
	commonInList1and2 = twoListCompare(myList1,myList2);
	commonInList1and3 = twoListCompare(myList1,myList3);
	commonInList2and3 = twoListCompare(myList2,myList3);

	
	myList3.removeAll(commonInList2and3);
	myList3.removeAll(commonInList1and3);
	
	
	
	myList2.removeAll(commonInList2and3);
	myList2.removeAll(commonInList1and2);
	
	myList1.removeAll(commonInList1and3);
	myList1.removeAll(commonInList1and2);
	
	for (String string : myList1) {
		System.out.println("only in list 1 :::"+string);
	}
	
	for (String string : myList2) {
		System.out.println("only in list 2 :::"+string);
	}
	
	for (String string : myList3) {
		System.out.println("only in list 3 :::"+string);
	}
	
	for (String string : commonList) {
		System.out.println("common in all lists :::"+string);
	}
	
	for (String string : commonInList1and2) {
		System.out.println("In 1 and 2 :::"+string);
	}

	for (String string : commonInList1and3) {
		System.out.println("In 1 and 3 :::"+string);
	}
	for (String string : commonInList2and3) {
		System.out.println("In 2 and 3 :::"+string);
	}
}

private static List<String> twoListCompare(List<String> myList1,List<String> myList2){
	List<String> commonList = new ArrayList<String>();
	for(String list2Val : myList2){
		for(String list1Val : myList1){
			if(list2Val.equalsIgnoreCase(list1Val)){
				commonList.add(list2Val);
			}
		}
	}	
	return commonList;
}
}

Output:

only in list 1 :::God
only in list 3 :::Guru
common in all lists :::Java
In 1 and 2 :::PHP
In 1 and 3 :::26
In 2 and 3 :::C

Java Recommended Books:

Apr 08

Java Regex Pattern Example

Regex [Regular Expression]:

Regular expression is a useful one to match and extract only the required parts.
Eg:
Defining the phone number formats.
Removing unreadable or unwanted characters from a string

Sample Program:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {
 
	public static void main(String[] args) {
		String inputStr = "?Javadomain.in's a technical,java-blog";
		Pattern pattern = Pattern.compile("[\\s\\?\\,\\-\\']+");
		Matcher matcher = pattern.matcher(inputStr);
		boolean temp = matcher.find();
		System.out.println(temp);
		if (temp) {
			String aftRegex = inputStr.replaceAll("[\\s\\?\\,\\-\\']+", "_");
			System.out.println(aftRegex);
		} else {
			System.out.println("else");
		}

	}

}

Output:

true
_Javadomain.in_s_a_technical_java_blog

Recommended Java Books:

Apr 01

PDF Scrapping in Java Sample code

PDF Scrapping:
We may needed to scrap the pdf using java. This involves parsing tables and different areas of pdf.

How can we proceed ?
We can proceed this way,
1. Converting pdf to html/xml
2. Parsing the converted html/xml using the Jsoup open source Jar (Jsoup-1.7.2).

Converting pdf to html:

To convert pdf to html we can use pdfbox jar (opensource). Disadvantage with pdfbox jar is, it will not convert the tables as exactly as there and also it will convert all the things as

tag. Which is also not predictable one in some cases.

For more options on Apache pdfbox refer this,

http://pdfbox.apache.org/commandline/

Command:

java -jar pdfbox-app-1.8.4.jar ExtractText -html 1.pdf 1.html

Ensure pdfbox jar/1.pdf are exist in the same place. And run this command in the same path.

Converting Pdf to xml:

You can convert any pdf to xml using PDFTextStream Jar. Its free till some extend.

You can download the jar and also find the sample code here,

http://snowtide.com/downloads

Advantage with PDFTextStream jar is, it will convert any pdf to xml with x, y coordination values. So its easy to parse and take the values of tables using jsoup.

Thanks for reading this post…!