gpt4 book ai didi

java - 如何让 String Tokenizer 忽略文本?

转载 作者:行者123 更新时间:2023-12-01 19:09:11 24 4
gpt4 key购买 nike

我有这个代码:

public void readTroops() {
File file = new File("resources/objects/troops.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

try {
reader = new BufferedReader(new FileReader(file));
String text = null;

// repeat until all lines is read
while ((text = reader.readLine()) != null) {
StringTokenizer troops = new StringTokenizer(text,"=");
String list = troops.nextToken();
String value = troops.nextToken();
}

这个文件:

//this is a comment part of the text file//

Total=1

问题是 1) 我无法让它忽略//,//中的所有内容,并且无法让它通过它们之间的“ENTER”(行)进行读取。例如,此文本有效:

Total=1

所以我的问题是我应该在分隔符区域中输入什么,即。

StringTokenizer troops = new StringTokenizer(text,"=","WHAT GOES HERE?");

那么我怎样才能让 Tokenizer 忽略“ENTER”/新行,以及//之间的任何内容或类似的内容,谢谢。

ps.我不在乎你是否使用 String.split 来回答我的问题。

最佳答案

使用方法 countTokens 跳过没有两个标记的行:

while ((text = reader.readLine()) != null) { 
StringTokenizer troops = new StringTokenizer(text,"=");
if(troops.countTokens() == 2){
String list = troops.nextToken();
String value = troops.nextToken();
....
}else {
//ignore this line
}
}

关于java - 如何让 String Tokenizer 忽略文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com