0

I want my program to mingle to strings in an array. The strings are coming from a .dat file. I keep getting an index out of range error.

the input file : 3 xyz abc abc rstuvwxy rstuv ab

wanted output: axbycz rasbtcuavbwcxayb rasbtaubva

error i'm getting:

arbsctException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(Unknown Source) at test.main(test.java:39)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class test {

    public static void main(String[] args) throws FileNotFoundException {

        File file = new File("strings.dat");
        Scanner infile = new Scanner(file);
        String string1;
        String[] mingle = new String[2];
        int length;

        infile.nextLine();

        while (infile.hasNextLine()) {
            string1 = infile.nextLine();

            for (int i = 0; i < mingle.length; i++) {
                mingle = string1.toLowerCase().split("[\\s.]");
            }

            System.out.println(mingle[1] + mingle[0]);

            if (mingle[0].length() > mingle[1].length()) {
                length = mingle[0].length();
            }

            else if (mingle[1] == mingle[0]) {
                length = mingle[1].length();
            }

            else {
                length = mingle[1].length();
            }

            for (int i = 0; i < length; i++) {
                System.out.print(mingle[0].charAt(i % length));
                System.out.print(mingle[1].charAt(i % length));
            }
        }
        infile.close();
    }
}

Subsequent Error

arbsctException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(Unknown Source) at test.main(test.java:39)

  • Please post the exact error you are getting. – stelioslogothetis Sep 20 '17 at 18:37
  • In addition to the exact error and stacktrace, please include sample input. – Strikegently Sep 20 '17 at 18:38
  • arbsctException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(Unknown Source) at test.main(test.java:39) – Jerry Hudak Sep 20 '17 at 18:39
  • sample input : 3 xyz abc abc rstuvwxy rstuv ab – Jerry Hudak Sep 20 '17 at 18:39
  • Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Sep 20 '17 at 18:42
  • why do you use `infile.nextLine();` **before** your while loop? – Master Yoda Sep 20 '17 at 18:47
  • i know where it errors out and i know why i just cant find a way to fix it. on the second loop of the while and the 4th loop on the for loop it says index out of bounds because its looking for a 4th char in mingle[0] and that doesn't exist. im just not sure how to fix this – Jerry Hudak Sep 20 '17 at 18:49
  • i use infile.nextline() to skip an Int at the first line of the file – Jerry Hudak Sep 20 '17 at 18:50
  • You are setting `length` to the longer length of two strings and then you access their characters according to that longer length. This is bound to get you an index out of bounds error. You need to take the shorter length. – laune Sep 20 '17 at 18:53
  • your issue appears to be centered around here: `for (int i = 0; i < length; i++) { System.out.print(mingle[0].charAt(i % length)); System.out.print(mingle[1].charAt(i % length)); }` – Master Yoda Sep 20 '17 at 18:53

3 Answers3

1

The second for loop would not always work. Since the value of length would either be the length of mingle[0] or mingle[1], you cannot have both the array elements within the same loop.

For Example. Assume length of mingle[0] is 10 whereas length of mingle[1] is 11. Since length of mingle[1] is greater, the value of length would be '11'. In this case, at the 11th iteration of the for loop (i.e. when i=10) then

//At 11th iteration, i=10, length=11
    for (int i = 0; i < length; i++) {
      System.out.print(mingle[0].charAt(i % length)); //equivalent of mingle[0].charAt(10)
      System.out.print(mingle[1].charAt(i % length));//equivalent of mingle[1].charAt(10)
    }

Since characters in mingle[0] can be accessed from 0-9, when accessing the 10th element you get the java.lang.StringIndexOutOfBoundsException

gautam
  • 21
  • 8
1

I guess that's what you want to do. Note that the loop uses the minimum of the lengths. You can print the excess of either string using substr.

private void merge( s1 String, s2 String ){
    int len = Math.min( s1.length(), s2.length() );
    for( int i = 0; i < len; ++i ){
        System.out.print( s1.charAt(i) + s2.charAt(i) );
    }
    System.out.println( s1.substr(len) + s2.substr(len) );
}
laune
  • 31,114
  • 3
  • 29
  • 42
0

#Python program

IndexError: list index out of range

example:

Team =["priya" ,"amanda" ,"sruthy"]

print(Team[3])

if you are trying to print the value at the position of 3,then you get index error this happens because that's just way beyond your list size and list index starts at zero.

  • I do not think this answer address the original question, more over it makes the assumption that the OP would be familiar with a different language (Python in this case). Finally there is no explanation as to why the index 3 would out of bounds for an array containing three elements, i.e that array index start at 0. – Gavin Mar 23 '21 at 07:53
  • If an array contains 3 elements, they re numbered 0 through 2. Element 3 is out of range. – NomadMaker May 20 '21 at 05:53