I am trying to make different strings print based off of the number of letters in the word, but my second "if" statement is breaking the code.
import java.util.Scanner;
public class practice {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter a string: ");
String aString = in.next();
int length = aString.length();
char first = aString.charAt(0);
char second = aString.charAt(1);
char third = aString.charAt(2);
char fourth = aString.charAt(3);
char fifth = aString.charAt(4);
char sixth = aString.charAt(5);
char seventh = aString.charAt(6);
if (length == 7) {
System.out.println(String.format("Line 1: %s %s %s %s ", first, third, fifth, sixth));
System.out.println(String.format("Line 2: %s %s %s ", second, fourth, sixth));
System.out.println(String.format("Line 3: %s %s %s %s ", seventh, fifth, third, first));
}
else {
if (length < 7) {
System.out.println("the length is less than 7");
}
}
}
}
If I enter a word that has less than 7 letters I just get an error that reads "String index out of range: 6". How can I fix this?
char seventh = aString.charAt(6)that is breaking if your input does not have a 7th character.