带有哈希图的CSV

时间:2019-07-10 08:19:22

标签: java hashmap

我从哈希图开始,我想尝试包含来自csv文件的数据,而不是仅仅手动输入所有值。有帮助吗?

这是用于向体育场和城镇输入球队。它可以手动输入

import java.util.HashMap; 
import java.util.Scanner;
public class hashTutorial1 {
public static void main(String[] args) {
HashMap<String, String> teamLocations = new HashMap<String, String>();
HashMap<String, String> teamStadiums = new HashMap<String, String>();
String team;
String town;
String stadium;
int cont;
Scanner scan = new Scanner(System.in);
for(int i=0;i>=0;i++){
          if((i%2==0)&&(i!=0)  ){
              System.out.println(i+" "+"teams have been entered. Enter 0 to stop");
              cont = scan.nextInt();
              if(cont==0){
                  System.out.println("ending  input");
              break;
              }else{
                  System.out.println("input resuming");
              }
            }
          System.out.println("Enter a team");
          team=scan.nextLine();
          System.out.println("Enter its local town");
          town=scan.nextLine();
          System.out.println("Enter its stadium");
          stadium=scan.nextLine();
          teamLocations.put(team, town);
          teamStadiums.put(team, stadium);
    }
for (String i : teamLocations.keySet()) {
  System.out.println("TEAM: " + i + " TOWN: " + teamLocations.get(i) + " STADIUM: " + teamStadiums.get(i));

}

    }

}

我希望将输出球队名称,体育场和城镇。

1 个答案:

答案 0 :(得分:0)

具有类似csv的结构

team;town;stadium

您可以使用BufferedReader的{​​{1}}逐行读取文件,然后使用readLine()的{​​{1}}获得以下数组:String 。然后,您可以将结果存储在这样的HashMap中(假设该数组称为array):

split(";")