此问题在此处已有答案:
PatternSyntaxException: Unclosed group with substring method(1个答案)
昨天就关门了。
我一直在制作一款受口袋妖怪启发的格斗游戏,其中.txt文件用于定义团队,格式为**“name”health(“atkName”损坏,“atkName”损坏)例如“Guy”90(“出拳”10,“踢腿”20,“大声叫喊”10)**我对解码器脚本有问题,一个是我在第43行的索引4附近得到了一个关于未闭组的“regex”错误。
下面是团队类:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Team {
private ArrayList<Character> team;
private String name;
public Team(String filename, String ext) throws IOException {
this.team = new ArrayList<Character>();
File account = new File("src\\" + filename + ext);
int created = 0;
if (account.createNewFile()) {
created = 1;
}
FileInputStream fileIn = new FileInputStream(account);
PrintWriter fileWr = new PrintWriter(account);
Scanner accountReader = new Scanner(fileIn);
if (created == 1) { // I'll later add a random fighter here
fileWr.println("\"Cheesey old man\" 90 (\"Yeet fish\" 20, \"Cringe meme\" 30, \"Actually good meme\" 20, \"Amazing meme\" 40)");
}
fileWr.flush();
fileWr.close();
while (accountReader.hasNextLine()) {
String charDat = accountReader.nextLine();
String charName;
int charHealth;
ArrayList<Attack> atks;
if (!charDat.isEmpty()) {
charDat = charDat.substring(charDat.indexOf("\"") + 1);
charName = charDat.substring(0, charDat.indexOf("\""));
charDat = charDat.replaceFirst(charName + "\" ", "");
charHealth = Integer.parseInt(charDat.substring(0, charDat.indexOf("(") - 1));
// HERE VVVVVVVV
charDat = charDat.replaceFirst(charHealth + "".concat(" ("), "");
// HERE ^^^^^^^^
atks = new ArrayList<Attack>();
while (charDat.charAt(0) != ')') {
if (charDat.charAt(0) == ',') {
charDat = charDat.replaceFirst(", ", "");
}
System.out.println(charDat);
charDat = charDat.substring(charDat.indexOf("\"") + 1);
System.out.println(charDat);
String atkName = charDat.substring(0, charDat.indexOf("\""));
System.out.println(atkName);
charDat = charDat.replaceFirst(atkName + "\" ", "");
System.out.println(charDat);
int atkDamage;
if (charDat.indexOf(",") != -1) {
atkDamage = Integer.parseInt(charDat.substring(0, charDat.indexOf(",")));
charDat = charDat.replaceFirst(atkDamage + ",", "");
} else {
atkDamage = Integer.parseInt(charDat.substring(0, charDat.indexOf(")")));
charDat = charDat.replaceFirst(atkDamage + ",", "");
}
atks.add(new Attack(atkName, atkDamage));
charDat = charDat.replaceFirst(charHealth + "", "");
}
this.team.add(new Character(charName, charHealth, atks));
}
}
this.name = filename;
}
}
字符串
下面是角色和攻击类:
import java.util.ArrayList;
public class Character {
private String name;
private int maxHealth;
private int health;
private ArrayList<Attack> attacks;
public Character(String name, int health, ArrayList<Attack> atks) {
this.name = name;
this.maxHealth = health;
this.health = this.maxHealth;
this.attacks = atks;
}
}
public class Attack {
private String name;
private int damage;
public Attack(String name, int damage) {
this.name = name;
this.damage = damage;
}
public String getName() {
return this.name;
}
public int getDamage() {
return this.damage;
}
}
型
这里是主要的方法:
public class Main {
public static void main(String args[]) throws IOException {
Team team1 = new Team("Kreepy", ".txt");
}
}
型
1条答案
按热度按时间wqlqzqxt1#
replaceFirst
的第一个参数是一个正则表达式。如果你不知道这是什么,你显然不需要它。使用另一个方法(.replace
,它替换所有的占位符。注意.replaceAll
的第一个参数也是一个正则表达式,所以你也不需要它)。如果
.replace
的replace all方面不符合您的喜好,请使用.substring
和.indexOf
自己编写;这是一个单行方法。或者,通过转义“修复它”:x.replaceFirst(Pattern.quote(arg1), replacement)
是您想要的。如果您使用replaceFirst是因为它看起来更快-那肯定不是。只需使用.replace
。它替换所有,并且不涉及正则表达式。replaceFirst
和replaceAll
是坏方法的可怕名字,这些方法最好不存在。但它们确实存在,java不会轻易删除东西。你真正能做的就是不使用它们,并且知道.replace
取代了所有,除非你真的真的知道你在做什么,否则不应该使用replaceFirst
和replaceAll
,即使这样,可能只是用Pattern
和Matcher
来写。