我具有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 \}
或至少没有重复
最佳答案
之所以将不需要的数据放入集合中,是因为文件输入很复杂,并且需要处理规则来确定文件的哪些部分有效。您已声明自己具有启动触发器和停止触发器。
处理状态为:
还没好
就绪(到达启动触发器后)-在此状态下,读入您的地图
完成(到达停止触发器后)
目前尚不清楚什么值输入地图(关键是日期)。目前,我已将名称和金钱值串联起来:如果不正确,请解决此问题。注意,您需要使用“,”分割字符串。
此代码管理这些状态,并确保进入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 \}
关于java - 如何从文本文件创建树形图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48590964/