Jamie Ryu
Jamie Ryu

Reputation: 11

why it giving me an error String index out of range

I am writing a method that word-wrapping all lines that are longer than 60 characters. If a line contains 112 characters, the method should replace it with two lines : one containing the first 60 characters and another containing the final 52 characters.

This is the text file that I am working on:

EX9.txt

and this is the program that I wrote :

import java.util.*;
import java.io.*;

public class EX9
{
   public static void main(String[] args)throws FileNotFoundException
   {
      Scanner input = new Scanner(new File("EX9.txt"));
      wordWrap(input);
   }

   public static void wordWrap(Scanner input)
   {
       while(input.hasNextLine())
       {
           String line = input.nextLine();
           if(line.length() <= 60)
           {
               System.out.println(line);
           }
           else
           {
               int leng_line = line.length();
               int sixty = 60;
               int count = 0;
               for(int i = 0 ; i < leng_line / 60 ; i += 1)
               {
                   System.out.print(line.substring(i*60, sixty));
                   sixty += 60;
               }
               System.out.print(line.substring(sixty,leng_line));
               System.out.println();
           }
       }
   }
}

and It is saying that

123456789012345678901234567890123456789012345678901234567890
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -58

I am not sure why range is -58. What am I missing?

Upvotes: 1

Views: 112

Answers (3)

xiaodehenlang
xiaodehenlang

Reputation: 11

1 System.out.print(line.substring(i * 60, sixty)); --> System.out.println(line.substring(i * 60, sixty));

2 System.out.print(line.substring(sixty,leng_line)); --> System.out.println(line.substring(leng_line / 60*60, leng_line));

System.out.print(line.substring(sixty,leng_line)),sixty mabey > leng_line,so report error.

Upvotes: 1

sn-
sn-

Reputation: 488

Here, your code is failing at this line outside the while loop, Here you are incrementing the sixty every time in the loop and when you are out of the loop its value is more than the line's length.

System.out.print(line.substring(sixty,leng_line));  // leng_line > sixty

To avoid that you can subtract 60 from sixty.

System.out.print(line.substring(sixty-60,leng_line));

Complete code:

import java.util.*;
import java.io.*;

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

        Scanner input = new Scanner(new File("C:\\Users\\asn\\Desktop\\zzz.txt"));
        wordWrap(input);
    }

    public static void wordWrap(Scanner input) {

        while (input.hasNextLine()) {
            String line = input.nextLine();
            if (line.length() <= 60) {
                System.out.println(line);
            } else {
                int leng_line = line.length();
                int sixty = 60;
                int count = 0;
                for (int i = 0; i < leng_line / 60; i += 1) {
                    System.out.println(line.substring(i * 60, sixty));
                    sixty += 60;
                }
                System.out.println(line.substring(sixty-60, leng_line));
            }
        }
    }
}

Upvotes: 1

Pallav Khare
Pallav Khare

Reputation: 481

Kindly update the for loop condition to resolve this issue as follows-

for(int i = 0 ; i < leng_line / 60 ; i += 1){
System.out.print(line.substring(i*60, i*60+sixty));
sixty += 60*i;
}

Upvotes: 1

Related Questions