0

I have made a program designed to run through a text document(Shakespeare's King Lear) and replace all instances of the letter s with z and "sir" with "dawg". I have the first method working, however I am having trouble figuring out what the problem is with my other method (meant to replace "sir").

Everything looks alright but it keeps saying "Out of Bounds". Any suggestions/errors in my code?

import java.util.Scanner;
import java.io.*;
public class KingLear
{
public static void main (String[] args) throws FileNotFoundException
{
    PrintStream ps = new PrintStream("new_lear.txt");
    Scanner fileScan = new Scanner(new File("king_lear.txt"));
    Scanner fileScan2 = new Scanner(new File("king_lear.txt"));
    String currentLine;
    String currentLine2;

    while (fileScan2.hasNextLine())
    {
        currentLine2 = fileScan.nextLine();

        ps.println(dawg(currentLine2));
    }


    while (fileScan.hasNextLine())
    {    
        currentLine  = fileScan.nextLine();

        ps.println(zReplace(currentLine));

    }
   }
  public static String zReplace (String line)
  {
    String newLine = "";
    for (int i = 0; i < line.length(); i++)
    {
        char letter = line.charAt(i+1);
        if (letter == 's')
            newLine += 'z';
        else if (letter == 'S')
            newLine += 'Z';
        else 
            newLine += letter;
    }
    return newLine;
}

public static String dawg (String line)
{
    String newLine = "   ";
    for (int i = 0; i < line.length(); i++)
    {
        char letter = line.charAt(i);
        if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
        {
            newLine +="dawg";
        }

    }
    return newLine;
}
}
  • Change `for (int i = 0; i < line.length(); i++)` to `for (int i = 0; i < line.length() - 2; i++)` – artm Oct 28 '14 at 01:14

4 Answers4

2

No need to reinvent the wheel. Just use String.replace instead of complicating things.

line = line.replace("sir", "dawg");

All the replacement logic you have so far can be rewritten as:

line = line.replace("s", "z").replace("S", "Z").replace("sir", "dawg");
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

When you go through each line, you go char by char. Right here:

for (int i = 0; i < line.length(); i++)
{
    char letter = line.charAt(i);
    if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
    {
        newLine +="dawg";
    }

}

But if you are on the last char. It will read:

i = line.length() - 1;
char letter = line.charAt(i);
if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
{
    newLine +="dawg";
}

This is a problem because line.charAt(i+2) will be checking for char that doesn't exist. (it is in fact 2 places too far.)

To fix this change:

for (int i = 0; i < line.length(); i++)

to:

for (int i = 0; i < line.length() - 2; i++)

Now it won't read too far. This should fix your issue. Hope this helps :)

Edit: This should explain your error with it just having dawgs.

To fix the error of it just printing dawg you also need to append the other letters to newLine like this:

for (int i = 0; i < line.length() - 2; i++)
{
    char letter = line.charAt(i);
    if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
    {
        newLine += "dawg";
        i += 3;
    }
    else if (i == line.length() - 3){ // checks if this is the last possible dawg
        newLine += line.charAt(i);
        newLine += line.charAt(i + 1);
        newLine += line.charAt(i + 2); // adds the last 3 chars to the string
    }
    else{
        newLine += line.charAt(i); // adds text other than dawg to newLine
    }

}

Using this method it should work just how you want it to. However much like Robby Cornelissen said I would look into the String.replace() function, because it can be very useful and is much more readable.

Stephen Buttolph
  • 643
  • 8
  • 16
  • Tried this and I don't get the error message anymore, but when I go to the document it just says "dawg" over and over. What else could have gone wrong...? – Zander Maitland Oct 28 '14 at 17:08
  • @ZanderMaitland If you look back to your `zReplace` function you have an `else` statement that adds text if it isn't a s or S, you need to do the same here. I've added an example. – Stephen Buttolph Oct 28 '14 at 21:07
0

in your for loop you will really get outofbounds

String newLine = "";
for (int i = 0; i < line.length(); i++)
{
    //in this line
    char letter = line.charAt(i+1);
    if (letter == 's')
        newLine += 'z';
    else if (letter == 'S')
        newLine += 'Z';
    else 
        newLine += letter;
}

change it to:

// minus another 1 index
for (int i = 0; i < line.length() - 1; i++)
{
    //in this line
    char letter = line.charAt(i+1);
    if (letter == 's')
        newLine += 'z';
    else if (letter == 'S')
        newLine += 'Z';
    else 
        newLine += letter;
}

and for your "dawg" function change it too:

String newLine = "   ";
    //minus 2 index
    for (int i = 0; i < line.length()-2; i++)
    {
        char letter = line.charAt(i);
        if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
        {
            newLine +="dawg";
        }

    }

you are getting out of bounds because you are accessing an index out of the array using those "+1 or +2" in index.

Ker p pag
  • 1,568
  • 12
  • 25
0

Its hard to tell waht is causing the exception. Just by looking at the code seems like this line may be causing the issue

if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')

Notice for each i you are checking postions i+1 and i+2. This will result in ArrayOutOfBoundsException when i = line.length-1 or i = line.length-2

Chiseled
  • 2,280
  • 8
  • 33
  • 59