How to read a URL using Java?


  • Below program consists of steps to read data from the URL
package in.malliktalksjava;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
* @author malliktalksjava
*
*/
public class URLConnectionReader {

public static void main(String[] args) throws Exception {

URL oracle = new URL("http://malliktalksjava.in/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Merge Sort Example in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sorts the given array using merge sort algorithm
*/
public class MergeSortExample {

private int[] array;
private int[] tempMergArr;
private int length;

/**
* @param args
*
* main method.
* Pass the array to sort method and prints the output array into console.
*/
public static void main(String args[]){

int[] inputArr = {12,25,36,95,86,21,14,52,85,32};
MergeSortExample mergeSorter = new MergeSortExample();
mergeSorter.sort(inputArr);
for(int sortedValue:inputArr){
System.out.print(sortedValue);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is null of not, then pass it to mergeSort Method.
*/
public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
System.out.println(“Given Array is null or does not contain any elements”);
return;
}

this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
mergeSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the array using mergesort algorithm.
*/
private void mergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex – lowerIndex) / 2;
// sort the left side of the array
mergeSort(lowerIndex, middle);
// sort the right side of the array
mergeSort(middle + 1, higherIndex);
// merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}

/**
* @param lowerIndex
* @param middle
* @param higherIndex
*
*
*/
private void mergeParts(int lowerIndex, int middle, int higherIndex) {

for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}

 

Other Useful Links:

Quick Sort Example Program in Java

Insertion Sort Example program in JAVA

Selection Sort Example Program in Java

Bubble Sort Example in JAVA

Bubble Sort Example in JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

public static void bubbleSort(int arr[]) {
 int count = array.length;
 int var;
 for (int i = count; i >= 0; i--) {
   for (int j = 0; j  array[j+1]) {
          swapNumbers(temp2, var, array);
      }
   }
 printNumbers(array);
 }
}

private static void swapNumbers(int var1, int var2, int[] array) {
   int temp = array[var1];
   array[var1] = array[var2];
   array[var2] = temp;
}

private static void printNumbers(int[] input) {
  for (int i = 0; i < input.length; i++) {
    System.out.println(input[i] + ", ");
  }
}
}

Other Useful Links:

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java

Number series example program using JAVA


package in.malliktalksjava;

/**
* @author malliktalksjava.in
*
* This program prints the numbers in below pattern
* 1
* 123
* 12345
* 1234567
* 12345
* 123
* 1
*/
class PrintingNumberPatterns{

public static void main(String[] args){
printNumberSeries();
}

/**
* Print the numbers in assending and decending order by iterating it.
*/
private static void printNumberSeries() {
for (int i = 1; i <= 7; i += 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}

for (int i = 5; i >= 1; i -= 2) {
for (int j = 1; j < i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}