使用CSV文件中的整数初始化相应的整数

时间:2017-01-16 07:04:15

标签: java arrays csv

我正在开发一个基于文本的游戏,用完了模拟Shadowrun中的Decking的命令提示符。理想情况下,玩家应该能够拥有包含所有角色相关信息的CSV文件(65行,2列),并且该信息应该能够上传到该程序中。

我正处于正确加载csv文件的位置,并且仍在循环中正确显示所有内容。我不能为我的生活弄清楚如何从循环中获取值并进入我可以用来设置各种角色统计数据的值。我的代码片段如下:

public class Program {
static int[][] Character = new int[65][2];

public static void main (String[]args) {
  Scanner user_input = new Scanner(System.in);
  String DeckerName;
  System.out.println("What is your name? (Name of your .csv file)"); //Load Decker character
  DeckerName = user_input.nextLine();
  System.out.println("Alright, " + DeckerName + ".  Jacking into the Matrix now...");

  String csvFile = "FILE LOCATION";
  BufferedReader br = null;
  String line = "";
  String cvsSplitBy = ",";
  Scanner loader = new Scanner(System.in);

//Opens and loads .csv file into an array      
  try {
   br = new BufferedReader(new FileReader(csvFile));
   while ((line = br.readLine()) != null) {
    String characterload[] = line.split(cvsSplitBy);
    System.out.println ("" + characterload[0] + ": " + characterload[1]); // for testing purposes, characterload[0] is a text description, characterload[1] is an integer
    int intDeckerProgram = Integer.parseInt(characterload[1]);
    System.out.println(intDeckerProgram); //for testing purposes
    for (int i = 0; i < characterload.length; i++)
    {
      Character[i][1] = intDeckerProgram;
      System.out.println(Character[i][1]); //for testing purposes
    }
   }
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (br!= null) {
      try {
        br.close();
      } catch (IOException e) {
    e.printStackTrace();
      }
    }
  }

  int DeckerDetectionFactor = Character[0][1];
  System.out.println(Character[0][1]); //for testing purposes
  System.out.println(DeckerDetectionFactor); //for testing purposes
  int DeckerHackingPool = Character[1][1];
  System.out.println(Character[1][1]); //for testing purposes
  System.out.println(DeckerHackingPool); //for testing purposes

TL; DR没有下载其他java包,如何将导入的值带入characterload数组,并将它们放入Character数组中,以便正确初始化我的整数?谢谢你的时间,我真的很感激!

编辑:底部的两个值DeckerDetectionFactor和DeckerHackingPool分别需要为6和9(此信息来自CSV文件)。我正在寻找一个关于如何确保将这些值正确加载到Character [] []数组中的方向,以便正确初始化变量而不是将其置于0。

1 个答案:

答案 0 :(得分:0)

在给出一些有用的建议之后,我最终从While循环中删除了内部For循环。设置行整数,然后递增,解决了整个问题。如果它对任何人有帮助,这里是我现在使用的代码:

...
   br = new BufferedReader(new FileReader(csvFile));
   int row = 0;
   while ((line = br.readLine()) != null) {
    String characterload[] = line.split(cvsSplitBy);
    int intDeckerProgram = Integer.parseInt(characterload[1]);
      Character[row][1] = intDeckerProgram;
      row++;
   }
...