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));
        }
    }
}