【问题标题】:How can i copy tab seperated contents from txt file into Hashmaps?如何将制表符分隔的内容从文本文件复制到 Hashmap?
【发布时间】:2020-05-21 12:43:34
【问题描述】:

例如

FirstName LastName Balance
Bob     Blue        24.98
Steve   Green       345.98
Pam     White       0.00
Sue     Red         42.16
Scanner scanner = new Scanner(System.in);
Scanner input = new Scanner(System.in);
ArrayList < String > list = new ArrayList < String > ();
String data;
System.out.println("please enter the file path");
File myObj = new File(scanner.nextLine());
Scanner scan = new Scanner(myObj);
while (scan.hasNextLine()) {
    data = scan.nextLine();
    list.add(data);
}

我希望 FirstName、LastName 和 Balance 作为键,其余内容作为值。

【问题讨论】:

  • 提供您所做的尝试。
  • 您从哪里获得这样的价值?它是 JSON 还是只是一个字符串?
  • 从一个 txt 文件我得到这个值
  • 我使用此代码从 txt 文件中获取值并将值存储在列表中 Scannerscanner = new Scanner(System.in);扫描仪输入 = 新扫描仪(System.in); ArrayList list = new ArrayList();字符串数据; System.out.println("请输入文件路径"); File myObj = new File(scanner.nextLine());扫描仪扫描 = 新扫描仪(myObj); while (scan.hasNextLine()) { 数据 = scan.nextLine();列表。添加(数据); }
  • @XristosDjArchie,你想要一个三个项目,其中包括键 FirstName、LastName、Balance 和其他值是每个键的列表。

标签: java hashmap


【解决方案1】:

我已经编写了代码来回答您的问题,并且我稍微更改了 .txt 文件的格式(因此,每个值都用空格 (" ") 分隔标题和值)。我在下面给出了两个文件,并提供了预期的输出。我希望这会帮助你继续。如果您对答案有任何疑问,请告诉我。

FileReader.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class FileReader {
        public static void main(final String[] args) throws FileNotFoundException {
                final Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the file path");
                final String path = sc.next();
                File file = new File(path);
                Scanner fileScanner = new Scanner(file);
                String[] keys = fileScanner.nextLine().split(" ");
                List<String> firstNames = new ArrayList<>();
                List<String> lastNames = new ArrayList<>();
                List<String> balances = new ArrayList<>();
                while (fileScanner.hasNext()) {
                        String values[] = fileScanner.nextLine().split(" ");
                        firstNames.add(values[0]);
                        lastNames.add(values[1]);
                        balances.add(values[2]);
                }
                Map<String, List<String>> fileMap = new HashMap<>();
                fileMap.put(keys[0], firstNames);
                fileMap.put(keys[1], lastNames);
                fileMap.put(keys[2], balances);
                fileMap.entrySet().stream().forEach(e -> System.out.println(e));
                sc.close();
                fileScanner.close();
        }
}

TextFile.txt

FirstName LastName Balance
Bob Blue 24.98
Steve Green 345.98
Pam White 0.00
Sue Red 42.16

预期输出:

Please enter the file path
*\GitHub\playground\gvlab\src\TextFile.txt
FirstName=[Bob, Steve, Pam, Sue]
LastName=[Blue, Green, White, Red]
Balance=[24.98, 345.98, 0.00, 42.16]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多