Skip to main content

Posts

Convert String to Double in Java

String to Double in Java In this lecture we will see the String to Double conversion. There are three ways to convert a String into a double. 1: Using Double.parseDouble public static   double  parseDouble ( String  str ) throws NumberFormatException It returns the  double  value represented by the string argument and throws following exceptions: NullPointerException   – if the specified String str is null. NumberFormatException  – if the string format is not valid.  For example If the string is “122.20ab” this exception will be thrown. String strg = "222.102" ; Double var = Double . parseDouble ( str ); Double variable var value would be 222.102 after conversion. 2: Using Double.valueOf String strg1 = "222.102" ; Double var1 = Double . valueOf ( strg1 ); The value of var1 would be 222.102 3: Double class constructor String strg2 = "222.102" ; Double var2 = new Double ( strg2 ); Double class has a constru

Convert int to String in Java

Int to String conversion There are two ways to convert an integer value to a String. 1)  Using String.valueOf(int i) : This method takes integer value as an argument and returns a string representing the int augment. Method declaration : public static String valueOf(int i) parameters : i – integer that needs to be converted to a string returns : A string representing the integer argument int var = 123 ; String strg = String . valueOf ( var ); 2)  Using Integer.toString(int i) : This method works same as String.valueOf(int i) method. It belongs to the Integer class and converts the specified integer value to String. for example if passed value is 123 then the returned string value would be “123”. Method declaration : public static String toString(int i) parameters : i – integer that requires conversion returns : String representing the integer i. int var = 200 ; String strg = Integer . toString ( var ); Example: This program demonstrates the use

Convert String to int in Java

How to convert string to int in Java During work suddenly we need to make check on int value but in first we take values in string which is not required by logically.. for this we need to convert string in int then we can compare this int into string to compare right value. we are using parseInt() with argument to convert string into string   Using Integer.parseInt String strg = "5678" ; int num = Integer . parseInt ( strg ); The value of num would be 5678. Note :  All characters in the String must be digits however the first character can be a minus ‘-‘ sign.  String str = "-5678" ; int num = Integer . parseInt ( str ); The value of num would be -5678 Integer.parseInt throws NumberFormatException  If the String is not valid for conversion for example String str = "15ab" ; int num = Integer . valueOf ( str ); This set of statement would throw  NumberFormatException . you would see a compilation error like this: E

Convert String Object to Boolean in java

Convert String Object to Boolean Object Here we are gonna see two methods of String to convert Boolean object. we are checking here how to perform functionality of Boolean while having data in string class StringObj { public static void main ( String [] args ) { // String Object String strg = "true" ; String strg1 = "false" ; //Passing string value to the constructor  of Boolean class. Boolean obj = new Boolean ( strg ); Boolean obj1 = new Boolean ( strg1 ); System . out . println ( obj ); System . out . println ( obj1 ); // Using valueOf() method of Boolean class Boolean bobj2 = Boolean . valueOf ( str ); Boolean bobj3 = Boolean . valueOf ( str1 ); System . out . println ( bobj2 ); System . out . println ( bobj3 ); } } Output: false true false true Boolean valueOf(String s) Returns a Boolean with a val

String and its methods in java

Java String Class and its methods Java String provides a lot of concepts that can be performed on a string such as compare, concat , equals, split, length, replace, compareTo, intern, substring etc. In java, string is basically an object that represents sequence of char values. Creating a String There are two ways to create a String in Java String literal Using new keyword String literal In java, Strings can be created like this: Assigning a String literal to a String instance: String string = "Welcome" ; String string1 = "Here" ; The problem with this approach : As I stated in the beginning that String is an object in Java. However we have not created any string object using new keyword above. The compiler does that task for us it creates a string object having the string literal (that we have provided , in this case it is “Welcome”) and assigns it to the provided string instances. But  if the object already exist in the memory i