Posts

Showing posts from March, 2013

Conversion of String to Int using Java with Exception class

Hi Everyone, this post will help you in understanding how to convert a String to an Int using Java. The string is given as an input from command line. If invalid string is given, an exception will be thrown using the exception class defined. The logic for converting from String to Int is as follows: If the string is null, invalid exception is thrown. Otherwise check if character of the String. If the first character is negative, a flag is set to indicate that the number is  negative. Convert the character to corresponding number. For every other number multiply the number with 10 to the power of order of number (starting from 0)   and add it to the previous number. For eg: for 23, add (3 * 10 ^ 0) and (2 * 10 ^ 1). If an invalid number is passed, the exception is passed. For eg: in the case "12h5", an exception will be called for 'h'. Return the number when all characters of string are processed. The program to convert String to Int is as follows: -

Sample program using HSQL database

Image
Hi Everyone, In this blog post, I'm specifying a sample program using HSQL database. HSQL Database stands for Hyper Structured Query Language Database. HSQL DB stores data in memory and is written completely in java. HSQL DB is mainly used for testing purposes. One of the main advantage of HSQL DB is that we need not have to install anything. To do program using HSQL DB, first we need to import the jar file and run the jar file as a server. Select the server option and click ok. This means now HSQL DB acts as a server where data and tables are stored in memory. The sample program to connect using HSQL DB is written below. package com.blog.database; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Iterator; import java.util.List; public class HSQLDatabase { public static void main(String[] args) { try { Strin

How to find whether a number is a power of multiple of 2 (bitwise)

Hi Everyone, This blog post will help you in teaching how to find whether a number is a power of multiple of 2.  This program helps in checking, whether a number is a power of 4, 16, 32 etc. For checking whether a number is a power of 2, refer my previous post - How to check whether a number is a power of 2 . When we are asked a question to check whether a number is a power of 16 or 32, the usual method which comes in our mind is to check using logarithms. But this method is not preferred when relating to  performance issues. So, we go by the bitwise method. It is as follows. The program is as follows: 1) First we check whether the number is a power of 2. If it is not, then we are returning false. 2) Then we have to right shift the number and " the other number which is a multiple of 2" until it is greater than 1 and count the number of shifts of each. 3) If the count of shifts of the number to be checked is a multiple of count of shifts of the number which is a