2017-01-12 37 views
0

我正在研究一個讀取文件並將名稱和分數存儲在兩個單獨數組中的程序,但我很掙扎。這是我迄今爲止所擁有的。 我創建了名稱名稱的數組,但我很困惑我將如何將名稱複製到數組的每個索引。讀取文件並將名稱和數字存儲在兩個數組中

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class ScannerReadFileSplit { 
    public static void main(String[] args) { 

File file = new File("NamesScore.txt"); 
String[] names = new String[100]; 
int[] scores = new int[100]; 
int i; 

try { 
     Scanner scanner = new Scanner(file); 
     while (scanner.hasNextLine()) { 
     String line = scanner.nextLine(); 
     String [] words = line.split("\t"); 
     for (String word: words) { 
      System.out.println(word); 
     } 
     } 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    } 
} 
} 

我的文本文件是:

John James  60 
Kim Parker  80 
Peter Dull  70 
Bruce Time  20 
Steve Dam  90 
+1

什麼名字?你希望我們知道這個文件是什麼樣子的? – shmosel

回答

1

首先,你將要初始化i0你把它聲明:

int i = 0; 

然後,分裂,你可以拉行之後將數據移出String[]並將其放入您的namesscores陣列中:

String [] words = line.split("\t"); 

// The first element in 'words' is the name 
names[i] = words[0]; 

// The second element in 'words' is a score, but it is a String so we have 
// to convert it to an integer before storing it 
scores[i] = Integer.parseInt(words[1], 10); 

// Increment 'i' before moving on to the next line in our file 
i++; 

不要忘記增加i如上所示。

有一些錯誤檢查,我掩飾了。在撥打split()後,您可能需要檢查words長度是2。另外請記住,如果Integer.parseInt()不能將分數列中的數據作爲整數解析,則可以拋出NumberFormatException

1

int l = 0; 
    while (scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    String [] words = line.split("\t"); 
    names[l] = words[0]; 
    scores[l] = Integer.parseInt(words[1]); 
    System.out.println(l + " - name: " + names[l] + ", score: " + scores[l]); 
    l++; 
    } 
1

我曾嘗試什麼來糾正你的代碼,並提供在線的意見,我覺得你已經出了問題。其實你很接近解決方案。揣摩你做了什麼爲輸出像

String[] words = line.split("\t"); 

行的這行代碼會給出兩個字符串後(因爲它會分裂它只有一個製表符分隔的名字和分數在你的文件中的行) 。你可以嘗試自己調試。就像簡單地打印價值一樣。例如

System.out.println(words[0]); 

這會幫助你進一步進步。

希望這會有所幫助。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class TwoArrays { 
    public static void main(String[] args) { 
     File file = new File("C:\\test\\textTest.txt"); 
     String[] names = new String[100]; 
     int[] scores = new int[100]; 
     int i = 0; 
     try { 
      Scanner scanner = new Scanner(file); 
      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       String[] words = line.split("\t"); 
       names[i] = words[0]; // storing value in the first array 
       scores[i] = Integer.parseInt(words[1]); // storing value in the 
                 // second array 
       i++; 
      } 
      /* 
      * This piece of code will give unnecessary values as you have 
      * selected an array of size greater than the values in the file for 
      * 
      * for(String name: names) { 
      *  System.out.println("Name:- "+name); 
      * } 
      * for(int score: scores) { 
      *  System.out.println("Score:- "+score); 
      * } 
      */ 
      // Better use the below way, here i am restricting the iteration till i 
      // i is actually the count of lines your file have. 
      for (int j = 0; j < i; j++) { 
       System.out.println("Name:- " + names[j] + "\t" + "Score:- " + scores[j]); 
      } 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
相關問題