Sunday, December 21, 2014

How to open chrome inspector(Developer tool) in separate window

Follow these steps to open chrome inspector(Developer tool) in separate window
1. Launch Chrome Browser and press key (Ctrl + Shift + i)
2. Step 1 will launch the developer tool, Place your pointer on the dock button and long click it (some    seconds) or right & left mouse click depending on the browser version.
3. Click on the option displaying after step 2, this will open developers tool in new window.

Tuesday, July 29, 2014

Find the occurrence of Each character in an Given String

This Java code will Print the number of occurrence for all unique character in the given String

Input - Test@Tutor
Output  -
Occurrence of char [t] - 4
Occurrence of char [e] - 1
Occurrence of char [s] - 1
Occurrence of char [@] - 1
Occurrence of char [u] - 1
Occurrence of char [o] - 1
Occurrence of char [r] - 1


CODE -

public class CountCharOccurance
{
public static void main(String[] args)
{
String value = "Test@Tutor";

int[] count = new int[256];

value = value.toLowerCase();

for (int i = 0; i< value.length(); i++)
{
count[(int)value.charAt(i)]++;
}

List<Character> printed = new ArrayList<Character>();

for (int i = 0; i< value.length(); i++)
{
if (printed.contains(value.charAt(i)))
continue;

printed.add(value.charAt(i));

System.out.println("Occurrence of char [" + value.charAt(i) + "] - " + count[(int)value.charAt(i)]);
}
}

}

Converting ASCII to char and Char to ASCII

ASCII to Char
char char-value= (char) asciiValue;

Char to ASCII
int asciiValue = (int) char-Value

Input = 5245 the output should be displayed as 5000+200+40+5.

Java code to Format the given Integer Value

public class FormatIntValue
{
public static void main(String[] args)
{
int num = 5245;

int position = 1;
String outString = "";
String tempString = "";

while(true)
{
tempString = outString ;
outString = "";

outString += (num%10) * position;
position *= 10;
num /=10;

if (position != 10)
outString += "+";
outString += tempString;

if (num < 10)
break;

}
tempString = outString;
outString = "";

outString += (num)*position;
outString += "+";
outString += tempString;

System.out.println(outString);
}
}

Java code to print Asterisks (*) based on Input Provides

Java code to print Asterisks (*) based on Input Provides

e.g: n = 5
then o/p will be as -








CODE:  

public class AstrikPrint
{
public static void main(String[] args)
{
int n = 5;
int count = 1;
for (int i = 0; i<n/2; i++ )
{

for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count+=2;

System.out.print("\n");
}

for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count-=2;

System.out.print("\n");

for (int i = 0; i<n/2; i++ )
{
for (int j = count; j >0; j--)
{

System.out.print("*");
}
count-=2;

System.out.print("\n");
}

}


}

Tuesday, July 8, 2014

Find the Size of longest subsequent array (Subsequent array must be in increasing order)

This Java Code will find the Size of longest subsequent array (Subsequent arraywill be in increasing order)

Example:-

     Input array : { 10, 22, 9, 33, 21, 50, 41, 60, 80 }
     the largest subsequent array will be - {10, 22, 33, 50, 60, 80}
 So the Output will be - 6 

CODE:-

import java.util.Scanner;

public class Test {   

    public static int longestSeq(int[] input1) {
        int longest = 0;
        int[] longestArray = new int[input1.length];

        for (int i = 0; i < input1.length; i++) {

            longestArray[i] = 1;
            int tempCount = i ;
            for (int j = tempCount+1; j < input1.length; j++ ) {               

                if (input1[tempCount] < input1[j]) {

                    longestArray[i]++;
                    tempCount = j;
                }
            }
        }

        longest = longestArray[0];
        for (int i = 1; i<longestArray.length; i++) {

            if (longest < longestArray[i])
                longest = longestArray[i];
        }
        return longest;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println ("Enter Size of an array: ");
        int n = input.nextInt();


        int[] valueArray = new int[n] ;   

        System.out.println ("Enter The array elements: ");

        for (int i = 0; i< n; i++) {

            valueArray[i] = input.nextInt();
        }   

        System.out.println("The Longest Subsequent Array size is: " + longestSeq(valueArray));

    }

}
 

Tuesday, July 1, 2014

Find the second smallest number in an given array

This Java Code will print the Second Smallest number from the specified Array list

import java.util.Scanner;

public class Test {   

    public static int second (int[] values) {

        int min = -1, secondMin = -1;
        int firstValue = values[0];
        int secondValue = values[1];
        if (firstValue < secondValue) {
            min = firstValue;
            secondMin = secondValue;
        } else {
            min = secondValue;
            secondMin = firstValue;
        }
        int nextElement = -1;
        for (int i = 2; i < values.length; i++) {
            nextElement = values[i];
            if (nextElement < min) {
                secondMin = min;
                min = nextElement;
            } else if (nextElement < secondMin) {
                secondMin = nextElement;
            }
        }
        return secondMin;

    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println ("Enter Size of an array: ");
        int n = input.nextInt();


        int[] valueArray = new int[n] ;   

        System.out.println ("Enter The array elements: ");

        for (int i = 0; i< n; i++) {

            valueArray[i] = input.nextInt();
        }   

        System.out.println("Second Sortest element in given array is: " + second(valueArray));

    }

}

Open a default browser in Window machine through simple java Code

import java.io.IOException;

public class Test {   

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

        String url = "http://www.google.com";
        Runtime runtime = Runtime.getRuntime();

        runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
    }

}

Reverse the Given Integer Number

This java Code will reverse the given Integer number. (Integer number will be Ignoring any 0 value at beginning of Given Integer or Reversed integer value)

import java.util.Scanner;

public class Test {   
    public static int reverseNumber (int value) {

        int tempValue = 0;

        while (value > 9) {

            tempValue = tempValue*10;
            tempValue += value%10;
            value = value/10;

            if (value <= 9) {
                tempValue = tempValue*10;
                tempValue += value%10;
            }
        }
        value = tempValue;
        return value;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println("Enter number to reverse : ");
        int value = input.nextInt();

        System.out.println("Reversed Numver: " + reverseNumber(value));
    }

}

Reverse the alternate words from given string

This Java code will reverse the alternate words from a given String.
Example:





import java.util.Scanner;

public class Test {   

    public static String reverseAltWord(String value) {

        String[] tempValue = value.split("\\s");       

        for (int j=0; j<tempValue.length; j=j+2) {

            String curValue = tempValue[j];
            char[] valueList = new char[curValue.length()];

            int k = 0;
            for (int i = curValue.length() -1; i>=0; i--) {

                valueList[k] = curValue.charAt(i);
                k++;
            }

            tempValue[j] = String.valueOf(valueList);
        }

        value = "";
        for (int i=0; i<tempValue.length;i++) {

            value = value + tempValue[i] + " ";
        }
       
        return value;               
    }

    public static void main(String[] args) {

        String value ;

        Scanner input = new Scanner (System.in);

        System.out.println ("Enter String Value to be reversed: ");
        value = input.nextLine();

        String reversedString = reverseAltWord(value);

        System.out.println("Reverse2 String is : " + reversedString);       
    }
}

Reverse a given String

This Java code will take the Input String from Console by the user and prints the reversed string into the Console.

input: qwerty
output: ytrewq

import java.util.Scanner;

public class Test {   
   
   public static String reverseString(String value) {
       
        char[] valueList = value.toCharArray();
       
        int j = 0;
        for (int i = value.length()-1; i>=0; i--) {
           
            valueList[j] = value.charAt(i);
            j++;
        }
       
        value = String.valueOf(valueList);
       
        return value;               
    }
  
    public static void main(String[] args) {
       
        String value ;
       
        Scanner input = new Scanner (System.in);
       
        System.out.println ("Enter String Value to be reversed: ");
        value = input.nextLine();
   
        String reversedString = reverseString(value);

        System.out.println("Reverse2 String is : " + reversedString);       
    }
}

Friday, June 27, 2014

Arrange integer number in order of negative(-) Zero (0) and Possitive(+) number

public class ArrangeNum {
   
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
       
        List<Integer> numList = new ArrayList<Integer>();
        List<Integer> posList = new ArrayList<Integer>();
        List<Integer> negList = new ArrayList<Integer>();
        Integer middleEle = -1;
        int n;
        System.out.println("Enter Size of an array: ");
        n = input.nextInt();
       
        System.out.println("Enter arra elements :");
        for (int i = 0; i < n; i++ ) {
           
            numList.add(input.nextInt());
           
        }
       
        for (int i = 0; i < n; i++) {
           
            if (numList.get(i) == 0)
                middleEle = 0;               
            else if ((numList.get(i)) > 0)
                posList.add(numList.get(i));
            else
                negList.add(numList.get(i));
        }
       
        numList.clear();
        numList.addAll(negList);
        if (middleEle == 0)
         numList.add(middleEle);
        numList.addAll(posList);
       
        System.out.println("New Number List -\n" + numList);
       
   
    }

}

Monday, June 23, 2014

Java Code To Perform Read/Write operation

import java.util.Scanner;

public class ReadWrite {

public static void main(String[] args) {

int num = 1;
String val = "aa";

Scanner input = new Scanner (System.in);

while ( num != 0) {

System.out.println ("Enter Number: ");
num = input.nextInt();
System.out.println ("You Entered: " + num);

}
  while (! val.equalsIgnoreCase("e")) {

System.out.println ("Enter String Value: ");
val = input.next();
System.out.println ("You Entered: " + val);

}
}
}

Java Code to get Read System Registry value and Write it in HTML file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RegReader {
 
    public static final HashMap<String, String> readRegistry(String location, List<String> keyList){
   
    HashMap<String, String> regKeyValue = new HashMap<String, String>();
   
    for (String key : keyList) {
   
    try {

    // Run reg query, then read output with StreamReader (internal class)
    Process process = Runtime.getRuntime().exec("reg query " + '"'+ location + "\" /v " + key);                                              

    StreamReader reader = new StreamReader(process.getInputStream());
    reader.start();
    process.waitFor();
    reader.join();

    // Parse out the value
    String[] parsed = reader.getResult().split("\\s+");
    if (parsed.length > 1) {
    regKeyValue.put(key, parsed[parsed.length-1]);
    }
    else
    regKeyValue.put(key, null);
   
    } catch (Exception e) {}
    }
   
    return regKeyValue;
    }
 

    static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw= new StringWriter();

        public StreamReader(InputStream is) {
            this.is = is;
        }

        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1)
                    sw.write(c);
            } catch (IOException e) {
            }
        }

        public String getResult() {
            return sw.toString();
        }
    }
 
    public static void createHTML(HashMap<String,String> datas) {

    try {
    String basePath = new File(".").getCanonicalPath();
    File f = new File(basePath + "/src" + "/Registry.html");

    BufferedWriter writer = new BufferedWriter(new FileWriter(f));
    writer.write("<html>");
    writer.write("<body>");

    for (String key : datas.keySet()) {

    writer.write("<span>" + key + " : " + datas.get(key) + "</span></br>");
    }
   
    writer.write("</body>");
    writer.write("</html>");
       writer.close();
    } catch (Exception e) {
    // TODO: handle exception
    }
    }
 
    public static void main(String[] args) {

      String location = "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS";
    List<String> keyList = new ArrayList<String>();
    //Add List of Keys for getting its value
      keyList.add("SystemSKU");
    //keyList.add("VersionExt");
   
        HashMap<String, String> value = RegReader.readRegistry(location, keyList);

        createHTML(value);
     
        for (String key : value.keySet()) {
       
        System.out.println(key + " : " + value.get(key));
        }
    }
}