Java Conversion of String Value to Integer, Float, Double & Boolean

  1. String to Integer conversion in Java
  2. String to Float conversion in Java
  3. String to Double conversion in Java
  4. String to Boolean conversion in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. String to Integer conversion in Java


String_Integer.java

import java.util.Scanner;
 
public class String_Int {
	
	Scanner scan;
	String num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("String to Integer");
		
		System.out.println("\nEnter the String (number) :");
		num = scan.nextLine();
	}
	
	void convert() {
		
		int val = Integer.parseInt(num);
		
		System.out.println("The Integer Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		String_Int obj = new String_Int();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

String to Integer

Enter the String (number) :
123
The Integer Value is : 123




B. String to Float conversion in Java


String_Float.java

import java.util.Scanner;
 
public class String_Float {
	
	Scanner scan;
	String num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("String to Float");
		
		System.out.println("\nEnter the String (number) :");
		num = scan.nextLine();
	}
	
	void convert() {
		
		float val = Float.parseFloat(num);
		
		System.out.println("The Float Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		String_Float obj = new String_Float();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

String to Float

Enter the String (number) :
123.4567
The Float Value is : 123.4567




C. String to Double conversion in Java


String_Double.java

import java.util.Scanner;
 
public class String_Double {
	
	Scanner scan;
	String num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("String to Double");
		
		System.out.println("\nEnter the String (number) :");
		num = scan.nextLine();
	}
	
	void convert() {
		
		double val = Double.parseDouble(num);
		
		System.out.println("The Double Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		String_Double obj = new String_Double();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

String to Double

Enter the String (number) :
123.456789123
The Double Value is : 123.456789123




D. String to Boolean conversion in Java


String_Boolean.java

import java.util.Scanner;
 
public class String_Boolean {
	
	Scanner scan;
	String num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("String to Boolean");
		
		System.out.println("\nEnter the String :");
		num = scan.nextLine();
	}
	
	void convert() {
		
		boolean val = Boolean.parseBoolean(num);
		
		if(val == true)
			System.out.println("The Positive Boolean Value is : " + val);
		else
			System.out.println("The Negative Boolean Value is : " + val);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		String_Boolean obj = new String_Boolean();
		
		obj.getVal();
		obj.convert();
	}
}

Sample Output

String to Boolean

Enter the String :
false
The Negative Boolean Value is : false
String to Boolean

Enter the String :
true
The Positive Boolean Value is : true
String to Boolean

Enter the String :
other than 'true', it will consider as 'false' only..
The Negative Boolean Value is : false






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com