我有以下txt文件的内容:
Some text I would like not to inclcude
#id: col1, name, money\
2017-01-02, Michael, 200 \
2017-01-01, Tobias, 300 \
2017-02-03, Susan, 400 \
2017-05-04, John, 200 \
... ... ...
Some text I would like not to inclcude
我想创建一个Treemap并将col1
作为我的密钥:
import java.io.*;
import java.util.*;
class earnings
{
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("example.txt"));
TreeMap<String, String> map = new TreeMap<String, String>();
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\t");
map.put(columns[0],columns[0]);
}
System.out.println(map);
}
}
使用Some text I would like not to inclcude
输出:
{=, 2017-01-01, Tobias, 300 \= 2017-01-01, Tobias, 300 \,
2017-01-02, Michael, 200 \= 2017-01-02, Michael, 200 \, 2017-02-03,
Susan, 400 \= 2017-02-03, Susan, 400 \, 2017-05-04, John,
200 \= 2017-05-04, John, 200 \, Some text I would like not to
inclcude= Some text I would like not to inclcude, Some text I would
like not to inclcude=Some text I would like not to inclcude}
没有Some text I would like not to inclcude
的输出仍有一些重复:
{=, 2017-01-01, Tobias, 300 \= 2017-01-01, Tobias, 300 \,
2017-01-02, Michael, 200 \= 2017-01-02, Michael, 200 \, 2017-02-
03, Susan, 400 \= 2017-02-03, Susan, 400 \, 2017-05-04,
John, 200 \= 2017-05-04, John, 200 \}
如何设置创建一个Some text I would like not to inclcude
并看起来像
{ 2017-01-01, Tobias, 300 \ 2017-01-02, Michael, 200 \ 2017-02-03, Susan, 400 \ 2017-05-04, John, 200 \}
或者至少没有重复
答案 0 :(得分:2)
您将不需要的数据放入集合的原因是您的文件输入很复杂,并且您需要处理规则来确定文件的哪些部分有效。你已经声明你有一个开始触发器和一个停止触发器。
处理的状态是:
目前尚不清楚地图中的值是什么(关键是日期)。现在,我已经连接了名称和金钱值:如果这不正确,请修复此问题。请注意,您需要使用&#34;,&#34 ;;
拆分字符串此代码管理这些状态,并确保进入Map的任何数据都是有效数据(在启动和停止触发器之间)
public class Earnings {
final static String START_TRIGGER = " Some text I would like not to inclcude";
final static String STOP_TRIGGER = " Some text I would like not to inclcude";
enum ProcessingState {
NOT_READY,
READY,
FINISHED;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("Example.txt"));
TreeMap<String, String> map = new TreeMap<String, String>();
ProcessingState processingState = ProcessingState.NOT_READY;
while (scanner.hasNextLine() && processingState != ProcessingState.FINISHED) {
String lineToProcess = scanner.nextLine();
if (processingState == ProcessingState.READY && lineToProcess.startsWith(STOP_TRIGGER))
processingState = ProcessingState.FINISHED;
if (processingState == ProcessingState.READY) {
String[] columns = lineToProcess.split(",");
map.put(columns[0],columns[1]+", "+columns[2]);
}
if (processingState == ProcessingState.NOT_READY && lineToProcess.startsWith(START_TRIGGER))
processingState = ProcessingState.READY;
}
System.out.println(map);
}
}
我测试了你的数据,这产生了:
{ 2017-01-01= Tobias, 300 \, 2017-01-02= Michael, 200 \, 2017-02-03= Susan, 400 \, 2017-05-04= John, 200 \}
答案 1 :(得分:0)
String s = "2017-01-01, Tobias, 300 ";
String[] split = s.split(",");
if (split[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")) {
System.out.println(split[0].trim());
}
在您的方法中,您可以执行以下操作:
while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split(",");
if(columns[0].trim().matches("\\d{4}-\\d{2}-\\d{2}")
map.put(columns[0],columns[0]);
}