Home Tutorial Java Corejava Dataconversion String Conversion from String to char

 
 

Share on Google+Share on Google+
Conversion from String to char
Posted on: September 3, 2010 at 12:00 AM
Advertisement
In this tutorial we will learn how to convert a string type data to char type data.

Conversion from String to char:

In this tutorial we will learn how to convert a string type data to char type data.

Description:

This program will take a String value from mystring variable. The line Character mychar = mystring.charAt(i); is used to find out the char value at specific index value of i which will be enter by the user. If entered index value is exceed from the length of the string than exception will occur.

Code:

import java.io.*;

class Stringtochar {
	public static void main(String[] args) {
		try {
			BufferedReader buffreader = new BufferedReader(
					new InputStreamReader(System.in));
			System.out.println("Enter the value of index:");
			int i = Integer.parseInt(buffreader.readLine());
			String mystring = "Welcome to Java";
			Character mychar = mystring.charAt(i);
			System.out.println("The char value at index " + i + " is: "
					+ mychar);
		} catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}
}
 

Output:

Download this code

Advertisement

Related Tags for Conversion from String to char:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com