Thursday, October 24, 2013

String to Integer Conversion in Java

here are different ways of converting String object into Integer in Java :
1) By using Intger.parseInt(String string-to-be-converted) method
This is my preferred way of converting it, Extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:

 //using Integer.parseInt
        int i = Integer.parseInt("123");
        System.out.println("i: " + i);

Integer.parseInt() method will throw NumberFormatException if String provided is not a proper number. Same technique can be used to convert other data type like float and Double to String in Java. Java API provides static methods like Float.parseFloat() and Double.parseDouble() to perform data type conversion.
2) There is another way of converting String into Integer which was hidden to me for long time mostly because I was satisfied with Integer.parseInt() method. This is an example of Factory method design pattern in Java and known as Integer.valueOf(), this is also a static method like main and can be used as utility for string to int conversion. Let’s see an example of using Integer.valueOf() to convert String into int in java.

//How to convert numeric string = "000000081" into Integer value = 81
int i = Integer.parseInt("000000081");
System.out.println("i: " + i);

It will ignore the leading zeros and convert the string into int. This method also throws NumberFormatException if string provided does not represent actual number. Another interesting point about static valueOf() method is that it is used to create instance of wrapper class during Autoboxing in Java and can cause subtle issues while comparing primitive to Object e.g. int to Integer using equality operator (==),  because caches Integer instance in the range -128 to 127.

Tuesday, October 1, 2013

Jar file Error In Java

Dear Friend this post to those people those are start learning java.When people are new in error so worry about this common error the error as below

java.io.IOException: Unexpected end of input stream
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:339)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:782)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:1585)
        at com.mysql.jdbc.Connection.<init>(Connection.java:524)
        at com.mysql.jdbc.Driver.connect(Driver.java:359)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:140)
        at com.aquafold.aquacore.scripts.ConnectionProperties.getConnection(Unknown Source)
        at com.aquafold.datastudio.mainframe.dialog.ConnectionDialog.onOk(Unknown Source)
        at com.aquafold.datastudio.mainframe.dialog.ConnectionDialog.access$600(Unknown Source)
 
 
 Solution for this error:-
 
( Your JVM is picking up an old version of Connector/J somewhere in it's classpath.)
 
Means you are add old version of my(e.g mysql-connector-java-5.1.18-bin.jar) 
 So you add latest version of jar fine than problem was solved.
 
 
thanks you for read welcome to add you suggestion.