Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MiddleCharacter {
- //Middle Characters
- //You will receive a single string. Write a method that prints the middle character. If the length of the string is even,
- //there are two middle characters.
- //Examples
- //Input
- //aString
- // Output:r
- //someText
- // Output: eT
- //3245
- // Output: 24
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input=scanner.nextLine()
- ;
- System.out.println(middleCharacter(input));
- }
- static char middleCharacter(String input){
- char middle;
- char [] array;
- for (int i=0; i<=input.length()/2; i++){
- array[] = input.charAt(i);
- }
- if(input.length()%2==0){
- middle=array[array.length/2];
- }
- if(!(input.length()%2==0)){
- middle=array[0];
- }
- return middle;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment