2017-01-16 23 views
0

我工作的一個基於文本的遊戲運行的是模擬裝飾在暗影狂奔命令提示符。理想情況下,玩家應該能夠獲得包含所有角色相關信息的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數組中的值,並把它們放到字符數組,這樣我的整數正確初始化?謝謝你的時間,我真的很感激!

編輯:DeckerDetectionFactor和DeckerHackingPool底部的兩個值分別需要是6和9(此信息是從CSV文件中提取的)。我在尋找如何確保這些值正確裝入字[] []數組所以變量正確初始化爲給被投入在0

+0

初看起來你的代碼似乎在做你想做的事,所以你面對任何錯誤? –

+0

不是錯誤在嚴格的意義上,但在底部的兩個值,DeckerDetectionFactor和DeckerHackingPool,需要是6和9,分別爲(從CSV)。用我寫的代碼,他們最終返回值0.我想我正在尋找一個小方向來確保Character [] []是它需要在循環之外的值。 – Aldanius

+3

我投票作爲題外話,因爲這是要求的工作代碼進行審查,以關閉此問題屬於上http://codereview.stackexchange.com –

回答

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++; 
    } 
...