有一个错误,第3部分需要在另一条线上设置
我无法找到我的错误。
我在哪里需要设置第3部分来解决这个问题?
import java.io.*;
public class javadruckdruckextended {
public static void main(String[] args) throws IOException {
//scanning
FileReader fr = new FileReader ("fax_user.csv");
BufferedReader br = new BufferedReader (fr);
FileWriter fw = new FileWriter("Druckausgabe123.txt");
BufferedWriter bw = new BufferedWriter (fw);
String textnow = br.readLine ();
String n = System.getProperty("line.separator");
while (textnow !=null) {
//Umlaute gegen ae,oe,ue tauschen
String text = textnow;
String textXreplaced = text;
textXreplaced = text.replace ("ä","ae");
String textXXreplaced = textXreplaced;
textXXreplaced = textXreplaced.replace ("ü","ue");
String textXXXreplaced = textXXreplaced;
textXXXreplaced = textXXreplaced.replace ("ö","oe");
String textXXXXreplaced = textXXXreplaced.replace("ß","ss");
textXXXXreplaced = textXXXreplaced;
//Text umformatieren, splitten, E-Mail, Nummer anhängen
String textreplaced = textXXXXreplaced.replace(" ", "");
String textreplacedtwo = textreplaced.replace("\"","");
String textreplacedthree = textreplacedtwo.replace(";","");
String textreplacedfour = textreplacedthree.replace(".",","); // Um das "Dr." bei Friedbert Maas wegzusplitten.
String [] parts = textreplacedfour.split(",");
/* Zur Fehlerkontrolle
* if (parts.length<3) { System.out.println(textnow);}
* else {
*/
String Email = "@bogestra.de";
String Zahlen = ";3";
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
if (parts.length < 2){
String Sonderfalleins = part2 + Email + Zahlen + part1;
bw.write (Sonderfalleins);
} //close if
else {
if (parts.length > 3) {
String part4 = parts[3];
String Sonderfallzwei = part3 + "." + part4 + Email + Zahlen + part1;
bw.write (Sonderfallzwei);
} //close if
else {
String part3zwei = parts[2]; //vielleicht Entfernung
String finished = part3zwei + "." + part2 + Email + Zahlen + part1;
textnow = br.readLine();
bw.write(finished + n );
bw.flush();
} //close else 2
}//close else 1
} //close while
textnow = br.readLine();
bw.flush();
bw.close();
br.close();
} //close static void main String
} //close class
答案 0 :(得分:0)
这里的作业和长度检查明显无序:
String part1 = parts[0]; String part2 = parts[1]; String part3 = parts[2]; if (parts.length < 2) { String Sonderfalleins = part2 + Email + Zahlen + part1; bw.write(Sonderfalleins); } else { if (parts.length > 3) { String part4 = parts[3]; String Sonderfallzwei = part3 + "." + part4 + Email + Zahlen + part1; bw.write(Sonderfallzwei); } //close if
也就是说,在检查实际存在之前,您正在引用parts[1]
。
如果parts.length < 2
则parts[1]
会抛出ArrayIndexOutOfBoundsException
。
parts[3]
也是如此。
这是一个可能的简单修复:
String part1 = parts[0];
if (parts.length < 2) {
String part2 = parts[1];
String Sonderfalleins = part2 + Email + Zahlen + part1;
bw.write(Sonderfalleins);
} else {
if (parts.length > 3) {
String part3 = parts[2];
String part4 = parts[3];
String Sonderfallzwei = part3 + "." + part4 + Email + Zahlen + part1;
bw.write(Sonderfallzwei);
} //close if