Home Tutorial Java Core Java get middle character

 
 

Java get middle character
Posted on: December 12, 2012 at 12:00 AM
In this tutorial, you will learn how to get middle character from the word.

Java get middle character

In this tutorial, you will learn how to get middle character from the word. Here is an example of getting the middle letter of the word entered by the user using Java. If you have a word string 'company' and you want to get the character 'p' from the word, then you can use the below code.

Example:

import java.util.*;

class GetMiddleCharacter
{
	public static void main(String[] args) 
	{
		Scanner input=new Scanner(System.in);
		System.out.print("Enter word: ");
		String str=input.nextLine();
		int len=str.length();
		int c=len/2;
		char ch[]=str.toCharArray();
		System.out.println("Middle Character is: "+ch[c]);
	}
}

Output:

Enter word: company
Middle Character is: p

Related Tags for Java get middle character:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.