1

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?

3
  • From a very cursory look, I believe it's the line char seventh = aString.charAt(6) that is breaking if your input does not have a 7th character. Commented Feb 15, 2016 at 3:57
  • 1
    put 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); inside if Commented Feb 15, 2016 at 3:59
  • Heyy that worked, thanks a bunch! Commented Feb 15, 2016 at 4:00

4 Answers 4

1

since i don't know the exact requirement i just resolved the error check below coede

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();


  if (length == 7) {
   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);
 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");
}
  }
}

}    
Sign up to request clarification or add additional context in comments.

Comments

1

First check the length, then access the characters. Also, you can use printf (instead of String.format and println). Something like

if (length >= 7) {
     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);

     System.out.printf("Line 1: %s %s %s %s%n", first, third, fifth, sixth);
     System.out.printf("Line 2: %s %s %s%n", second, fourth, sixth);
     System.out.printf("Line 3: %s %s %s %s%n", seventh, fifth, third, first);
 } else {
     System.out.println("the length is less than 7");
 }

Comments

1

I bet you entered a 6 letter word.

This is because of your line:

char seventh = aString.charAt(6); // It tries to look for a 7th position in a String that has only six positions

I'd put all of the char assignments into the if:

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();


  if (length == 7) {
       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);

     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");
    }
  }
}

Comments

1

Try below code, it is more generic.

            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();
                 char[] charArr=new char[10];
                 int length = aString.length();
            for(int i=0;i<length;i++){
                charArr[i] =   aString.charAt(i);
            }

              if (length == 7) {
                 System.out.println(String.format("Line 1: %s %s %s %s ", charArr[0], charArr[2], charArr[4], charArr[5]));
                 System.out.println(String.format("Line 2: %s %s %s ", charArr[1], charArr[3], charArr[5]));
                 System.out.println(String.format("Line 3: %s %s %s %s ", charArr[6], charArr[4], charArr[2], charArr[0]));
              }
              else {
               if (length < 7) {
                 System.out.println("the length is less than 7");
               }
                  }
            }

            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.