我制作了一个程序,旨在通过一个文本文件(莎士比亚的李尔王),用z替换字母s的所有实例,用“dawg”替换“先生”。我有第一种方法可行,但是我无法弄清楚问题与我的其他方法有什么关系(意味着要取代“先生”)。
一切看起来都不错,但一直说“超出界限”。我的代码中有任何建议/错误吗?
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;
}
}
答案 0 :(得分:2)
无需重新发明轮子。只需使用String.replace
而不是使事情复杂化。
line = line.replace("sir", "dawg");
到目前为止,您所拥有的所有替换逻辑都可以重写为:
line = line.replace("s", "z").replace("S", "Z").replace("sir", "dawg");
答案 1 :(得分:0)
当你浏览每一行时,你会通过char去char。就在这里:
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";
}
}
但如果您使用最后字符。它将读作:
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";
}
这是一个问题,因为line.charAt(i+2)
将检查不存在的字符。 (事实上它实际上是两个地方。)
要解决此更改:
for (int i = 0; i < line.length(); i++)
为:
for (int i = 0; i < line.length() - 2; i++)
现在它读得太远了。这应该可以解决您的问题。希望这会有所帮助:)
编辑:这应该解释你的错误,只是有了dawgs。
要修复它的错误,只需打印dawg,你还需要将其他字母附加到newLine
,如下所示:
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
}
}
使用此方法,它应该按照您希望的方式工作。然而就像Robby Cornelissen说的那样,我会调查String.replace()
函数,因为它可以非常有用并且更具可读性。
答案 2 :(得分:0)
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;
}
将其更改为:
// 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;
}
并且你的“dawg”功能也改变了它:
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";
}
}
你越界了,因为你使用索引中的“+1或+2”来访问数组中的索引。
答案 3 :(得分:0)
很难说是造成异常的原因。只是通过查看代码似乎这一行可能导致问题
if (line.charAt(i) == 's' && line.charAt(i+1) == 'i' && line.charAt(i+2) == 'r')
注意每个你正在检查位置i + 1和i + 2。这会在i = line.length-1
或i = line.length-2