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