java - 如何从文本文件创建树形图

标签 java

我有一个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      

我想创建一个树形图并将 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);
        }
    }

输出一些我不想包含的文本:

    {=,  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}

输出没有一些我不想包含的文本仍然有一些重复:

    {=,  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 \}

如何创建一个树形图,其中包含一些我不想包含的文本并且看起来如下:

{ 2017-01-01, Tobias,  300 \ 2017-01-02, Michael, 200 \ 2017-02-03, Susan, 400 \  2017-05-04,  John,     200 \}

或者至少没有重复

最佳答案

您将不需要的数据添加到集合中的原因是您的文件输入很复杂,并且您需要处理规则来确定文件的哪些部分是有效的。您已经声明您有一个开始触发器和一个停止触发器。

处理状态为:

  1. 尚未准备好
  2. 准备就绪(到达开始触发器后)- 在此状态下读入您的 map
  3. 已完成(到达停止触发器后)

尚不清楚 map 中包含什么值(关键是日期)。现在,我已连接名称和金钱值:如果不正确,请修复此问题。请注意,您需要使用“,”来分割字符串;

此代码管理这些状态,并确保进入 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/

相关文章:

java - 如何使 Tomcat 7 类的实例保持 Activity 状态

java - 通过应用获取表之间的父子关系

java - Jetty 上的静态部署

java - 为什么我不能在 AppCompatActivity 的函数外部使用 create 方法

java - 在 ProgressBar 上绘制一个字符串,例如 JProgressBar?

java - 获取 Ant 构建错误 : Cannot run program "python"

java - Spring 启动/MongoDB : expecting to have different URL for GET and DELETE

java - 如何在IntelliJ中调试Vaadin应用程序

java - 逐行绘制矩形

java - 指定 "other"