Skip to main content

Posts

Showing posts with the label java

Java : Read from Excel (xls) file

Reading from a text file is common. But, reading from an excel(.xls) file is not. Recently, I got to do so. Little bit Google search and experimentation resulted in the following. I used HSSF from Apache-POI. import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public void readFromXlsFile (String pathToFile, String sheetName){     try {         FileInputStream file = new FileInputStream(pathToFile);         HSSFWorkbook workbook = new HSSFWorkbook(file);         HSSFSheet sheet = workbook.getSheet(sheetName);         Iterator<Row> rowIterator = sheet.rowIterator();                     while (rowIterator.hasNext()) {  ...

Reverse Engineering : Convert class or jar to Java files

Previously, we have seen how to extract contents of .img file and also how to convert dex files to classes/jar . In this post, I'm going to show a method to covert class/classes/jar file to java file(s). To do so, we are going to use a tool, JD-GUI. It is a Java Decompiler, which supports all versions of Java, including java-7. Download the application from here , and extract it to a proper location. Now, open the terminal (in case of Linux) and run the following command: ./jd-gui It will open the GUI window of Java decompiler. Just drag-and-drop a class/jar file into this window. In the left panel, we can find the corresponding java files, arranged in proper package format. To save a single file, select it and choose "Save Source" from the top menu. And to save entire package, choose "Save all sources". Troubleshooting : You may ran into one of these problems, while trying to open jd-gui. Install the packages specified accordingly. Erro...

Java: Use BigInteger in for-loop

In my previous post , I mentioned a way to handle large integers by using BigInteger. Now I'm going to provide a very important usage of it. We often use for-loop. So here is the way to use it: Ordinary integers: for(int i = 1; i <= n; i++) {  //Task to do } BigInteger: for (BigInteger bi = BigInteger.valueOf(1);                 bi.compareTo(n) <= 0;                 bi = bi.add(BigInteger.ONE)) { //Task to do } here n is a BigInteger variable.

Java: BigInteger

Many of you might have wondered, as I did, how to deal with integers of say more than 50 digits or so. And might have thought about breaking the integer into parts or storing it in a string and parse letter-by-letter . Java provides a class for such large numbers: BigInteger . You can find it in the package: java.math.BigInteger We can do some operations like addition, subtraction, multiplication, division etc, but slightly in a different way. For detailed information on its usage, have a look at this .

Java : Redirect/Write console output to a file

Sometimes we would like to save the output from console into a file and to do this we use some stream writer/reader to read from console and write to a file. But this can be done in much easier way as follows. Add the following code at the beginning of the program: PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); Now the default behaviour of out is changed to PrintStream out , and the output is redirected to the file provided above. Edit: To reset the standard behaviour, add the following code System.setOut(stdout);

Ubuntu : Java alternatives

Ubuntu will include openJDK by default. If you install some other version or Sun JDK, then the first installation is set to be the default one. To check if there are any other java installations: update-java-alternatives -l Change the default to any of them: update-java-alternatives -s NAME-OF-IT

Openlogic : Open Source Software Database

Here is a open source software repository, which contains almost every version of the packages. This will be useful for all those who need any older version of the package, which sometimes is difficult to find. So, I'm sharing the link here: http://olex.openlogic.com/ Type the package name in search field and select the version you need.

Setting JVM Heap size at runtime

To set the JVM heap size, compile the program normally. For example, consider Runtime.java program. Compilation: javac Runtime.java Now, to set minimum heap size(let, 16 MB) required by JVM, run the program as follows : java -Xms16m Runtime  We can also restrict maximum size(let 512 MB) utilized by JVM: java -Xmx512m Runtime  Both these options can also be combined to specify upper and lower bounds of JVM heap size: java -Xms16m -Xmx512m Runtime Now you can run a program that requires huge computational space.

Words formed by selecting one letter from each set (set combinations)

//Input 1,2   3,4,5  6  -1  each set is entered line-by-line //-1 is the end of input //output will be 136,146,156,236,246,256 import java.io.*; import java.util.*; import java.lang.String; import java.util.StringTokenizer; public class Possibilities {     public static void main(String[] args) throws IOException {         String s;         ArrayList<String> a1 = new ArrayList<String>();         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         s = br.readLine();         if (!s.equals("-1"))             a1 = generatePossibilities(s);         System.out.println(a1);     }     public static ArrayList<String> generatePos...

Shortest Job First Scheduling using HEAP

// Input is from the file "input.txt" in the form (a1,t1),(a2,t2),(a3,t3)  (arrival time, time to complete ie, burst time) import java.io.*; import java.util.*; public class SJFHeap {     static class point {         double time;         double run;         int no;         double total;         point(int i, double a, double b) {             no = i;             time = a;             run = b;             total = 0;         }     }     static class MHeap {         private point[] Heap;         p...

Spell Checker using TRIE

Spell checker program using TRIE data structure. Dictionary source is from Dictionary.txt, which is to be in the same directory of the program, or else change the file name and location //Possible.java import java.io.*; import java.util.*; class TrieNode {     char letter;     TrieNode[] links;     boolean fullWord;     TrieNode(char letter, boolean fullWord) {         this.letter = letter;         links = new TrieNode[26];         this.fullWord = fullWord;     } } public class Possible {     static TrieNode createTree()     {         return(new TrieNode('', false));     }     static void insertWord(TrieNode root, String word)     {         int offset = 9...

Spell Checker

This is a program which takes a word as input and gives possible suggestions to correct the spelling of the word, by searching them from the dictionary of words(a text file Dictionary.txt). // Spell.java import java.io.*; import java.util.*; public class Spell {     static ArrayList<String> wordsList = new ArrayList<String>();     public static void main(String args[]) throws IOException     {         try {             long build1 = System.currentTimeMillis();             FileInputStream fstream = new FileInputStream("Dictionary.txt");             BufferedReader br = new BufferedReader(new InputStreamReader(fstream));             String ele;             ...

Anagram search from a file

// All the words are in the file Dictionary.txt import java.io.*; import java.util.*; public class Anag {     static String[] words;     public static void main(String args[]) throws IOException     {         Hashtable<String, List> ht;         try {             FileInputStream fstream = new FileInputStream("Dictionary.txt");             DataInputStream in = new DataInputStream(fstream);             BufferedReader br = new BufferedReader(new InputStreamReader(in));             List<String> lines = new ArrayList<String>();             String ele;             while ((ele = br.readLine...

Josephus Problem using Queue

//Josephus.java //Out of  n members, every m'th person will be eliminated import java.io.*; //Stack--------------- class Node<T> {     T value;     Node<T> link; } class Stack<T> {     Node<T> top;     public Stack() {         top = null;     }     public void push(T item) {         Node<T> n = new Node<T>();         n.value = item;         n.link = top;         top = n;     }     public T pop() {         T item;         item = top.value;         Node<T> n = top;         n = null;         top = top.l...

Queue using Stack

import java.io.*; //Node--------------- class Node<T> {     T value;     Node<T> link; } // Stack--------------- class Stack<T> {     Node<T> top;     public Stack() {         top = null;     }     public void push(T item) {         Node<T> n = new Node<T>();         n.value = item;         n.link = top;         top = n;     }     public T pop() {         T item;         item = top.value;         Node<T> n = top;         n = null;         top = top.link;         return item; ...

Generic Stack

import java.io.*; class Node<T> {     T value;     Node<T> link; } class Stack<T> {     Node<T> top;     public Stack() {         top = null;     }     public void push(T item) {         Node<T> n = new Node<T>();         n.value = item;         n.link = top;         top = n;     }     public T pop() {         T item;         item = top.value;         Node<T> n = top;         n = null;         top = top.link;         return item;     }     p...

Nearest Neighbor Search Using KdTree

//Input should be in input.txt file //10(2.1,4.3)(3.3,1.5)(4.7,11.1)(4.9,1.6)(5.0,12.3)(5.1,1.2)(6.7,3.3)(19.2,5.4)(20.5,7.9)(100.3,52) //The first number is the number of points, n, and following it are the coordinates of the n points in ascending order of the x-coordinate. import java.util.*; import java.lang.*; import java.io.*; import java.util.StringTokenizer; class KDNode {     int axis;     double[] x;     int id;     boolean checked;     boolean orientation;     KDNode Parent;     KDNode Left;     KDNode Right;     public KDNode(double[] x0, int axis0) {         x = new double[2];         axis = axis0;         for (int k = 0; k < 2; k++)             x[k] = x0[k];         Left = Right = Parent = null;         checked = false; ...