Posts

Showing posts from 2016

How to convert compressed XML image file to a readable(decompressed) XML file

Yesterday at office our team came across a new problem, there were some changes made by the development team, they passed the data to one of the engine and we had to validate that data. The data was populating in compressed non readable format. So our job was to take that data, convert it to readable XML format and validate using XPaths. Due to privacy issues i cannot share the data and the way we validated that data, but here you will find how to decompress that data in readable XML format. Heres the code to convert compressed XML image file to a readable (deompressed) XML file package sorting; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.zip.GZIPInputStream; import javax.xml.bind.DatatypeConverter; public class Decompression 

Integer.MAX_VALUE, Double.POSITIVE_INFINITY and Double.NaN in Java

I have three questions for you in form of loop, you have to write a simple declaration by which the loop will never terminate and will run infinitely. I have given the hint in the title itself.   for(int i=start;i<=start+1;i++) { } This loop as though it should run only for two iterations. But, by taking the advantage of hint i.e. Integer.MAX_VALUE you can make it run indefinitely as below. int start =Integer.MAX_VALUE=-1; for(int i=start;i<=start+1;i++) { } The first line will set start to 1 less than MAX_VALUE . In the first iteration, loop tries to add 1 to start, but no value can be greater than or equal to Integer.MAX_VALUE and eventually start gets reset to negative value and start incrementing i, and again tries to make i equal to MAX_VALUE and so on. Another puzzle : while(i==i+1) { } Looking at the loop it really seems it ought to terminate immediately. A number can never be itself plus 1, right ?. But

Integer Overflow

The loop : Inside Out Have a look at below code and guess what could be the output. public class Puzzle26  { public static final int END=Integer.MAX_VALUE; public static final int START=END-100; public static void main(String args[]) { int count = 0; for(int i=START;i<=END;i++) count++; System.out.println(count); } } At a first glance you might think this will print 100, off course because, END is 100 more than START. But look more carefully we are using less than equal to sign. So now you might guess it will print 101. Well, no. This program does not print a thing.  The reason is every integer is less than or equal to Integer.MAX_VALUE. And the moment our program increases Integer.MAX_VALUE there cannot be any value greater than that so it increments the variable i to  -2147483648 , which is the lowest integer value and then keeps incrementing it to  2147483647 i.e. the largest integer value and so on, and it goes

Text To Speech using VB Script

Image
You know the beauty of this program, it dont expect you to download any external .dll or jar or anything, all you have to do is write 4 lines of code and your Text to Speech program is ready, yes you read it right. Lets see how. Open a notepad, Type below code in it : Dim message, sapi message = InputBox("Write something and I will say it for you"+vbcrlf) Set sapi= CreateObject("sapi.spvoice") sapi.Speak message Save the notepad file with .vbs extension , you will notice that the file icon is changed to VB Script icon. N ow open the file again, it will open a window like below.  Type any text in the text box and it will say it for you..Bingo....!

Character behaviour in Java

Today we will discuss some coding practices with characters and Strings. Can you guess the output of the below code: public static void main(String args[]) { String str="stark: 8"; String str2= "stark: "+str.length(); System.out.println("strings are equal "+str==str2); } Many people will think the output will be strings are equal true , well, its not because whenever we use "==" operator, it checks whether the references of the two strings are equal or not, it doesn't check if the values are equal or not. So now you might be thinking that the output will be  strings are equal false, again the output will be different, if you ran the program now you will see  false. It doesn't print strings are equal.  where's our string literal now ? Here the compiler will treat the statement as below: System.out.println(("strings are equal "+str)==str2); This performs the concatenation between strings are equa

Data Structures programs in Java

In this post we will look at very important subject in computer science, every computer science student must know data structure and how to implement it. Not only in theory but he/she must know how to code it. Sounds boring !!!. Forget whatever I said above, just code for fun. There's no rule to follow in tech. Here's the code: Bubble Sort in java: from the Wikipedia article: Bubble sort , sometimes referred to as  sinking sort , is a simple  sorting algorithm  that repeatedly steps through the list to be sorted, compares each pair of adjacent items and  swaps  them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a  comparison sort , is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to  insertion sort . [1]  It can be p

Working with Expression in Java

As you might have figured it out, today I am going to share some code playing with expressions, simple arithmetic, conversion and typecasting. Compound statement, typecasting and assignment. x+=i; is not equal to x=x+i; Many programmers might think that above two statement are identical, talk is cheap let me show you the code... public static void main(String args[]) { short x=0; int i=123456; System.out.println(x+=i); System.out.println(x=x+i); } Here we thought the first println will print output 123456, but thats not true, after the execution it shows -7616, the int i is too big to fit in short x, the automatically generated cast lops off the two higher-order bytes of the int value, because the JLS says... x+=i; means x=(type of x)(x+i); Here the statement gets automatically typecasted to operand x. In other words "compound assignment expressions automatically cast the result of the computation

Java Numeric Promotion

Moved to http://www.projectdebug.com/2017/09/12/java-numeric-promotion/