-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiddleThree.java
More file actions
23 lines (22 loc) · 883 Bytes
/
Copy pathMiddleThree.java
File metadata and controls
23 lines (22 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Given a string of odd length, return the string length 3 from its middle,
so "Candy" yields "and". The string length will be at least 3.
*/
package codingbat;
import java.util.Scanner;
public class MiddleThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str="";
int middle;
while(str.length() % 2 == 0){
System.out.println("Enter a string of odd length and length more than three: ");
str = sc.nextLine();
if(str.length() % 2 == 0 || str.length() < 3){
System.out.println("String of EVEN LENGTH and LENGTH LESS THAN 3 is NOT ALLOWED.\n");
}
}
middle = str.length() / 2;
System.out.println("Result: " + str.charAt(middle-1) + str.charAt(middle) + str.charAt(middle+1));
}
}