我编写了一个程序,用于运行一个文本文档(莎士比亚的“李尔王”),并将字母s的所有实例替换为z,将“先生”替换为"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;
}
}发布于 2014-10-28 09:14:08
不需要重新发明轮子。只需使用String.replace,而不是使事情复杂化。
line = line.replace("sir", "dawg");到目前为止,您拥有的所有替换逻辑都可以重写为:
line = line.replace("s", "z").replace("S", "Z").replace("sir", "dawg");发布于 2014-10-28 09:16:13
当您遍历每一行时,您将逐一查看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()函数,因为它非常有用,可读性也更好。
发布于 2014-10-28 09:16:22
在您的for循环中,您将真正获得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“访问数组外的索引。
https://stackoverflow.com/questions/26599368
复制相似问题