2013-03-17 23 views
0

我需要幫助製作一個數組列表,以便我可以找出數組中需要多少元素,當我從文本文件中讀取它們時。我還沒有學會如何做到這一點,幫助將不勝感激。如果我沒有正確存儲在數組中,我將顯示需要修復的代碼部分,因爲我得到空值。這篇文章是需要幫助的,但下面是完整的代碼。找出我從文件中得到多少個數組元素(不知道列表)

public static void output(tokens[], correct[], percentage[], letterGrade[], double  totalAVG, int highScore, int lowScore) 
{ 
PrintWriter pw = new PrintWriter(new FileWriter("Result.txt")) ; 

for (int t=0 ; t< tokens.length ; t+=2) 
{ 
    g = 0 ; 
    pw.println(tokens[t] + "," + correct[g] + percentage[g] + letterGrade[g]) ; 
    g++ ; 
} 
    pw.println("Average: " + totalAVG + "% (" + totalGrade + ")") ; 
    pw.println("High Score: " + highScore*2) ; 
    pw.println("Low Score: " + lowScore*2) ; 

pw.close() ; 





import java.util.* ; 
import java.io.* ; 

public class Proj5 
{ 

public static void main(String[] args) throws IOException 
{ 

Scanner s = new Scanner(System.in) ; 

/*這片打開的連接與該文件,將裂變各行,然後放 片到一個數組(令牌)。 */

String[] tokens= information(fileCheck) ; 

/* * * 打開連接與IDS和答覆文件,並返回他們分手了。 * @param(String a)獲取文件名以用於方法 * @return返回包含分割文件的數組。 */

public static String[] information(String a) throws IOException 
{ 
    Scanner inFile = new Scanner (new File(a)) ; // opens connection with file 
    String[] quarters = new String[] ; 
    int index = 0 ; 
    int lengthArray = 0 ; 
    while (inFile.hasNext()) 
    { 
     lengthArray++ ; 
    } 
    while (inFile.hasNext())       

//循環而更多的線在文件 {

 String line = inFile.nextLine() ;   // brings in next line to be broken up 
     String[] array = line.split(",") ; 
     quarters[index] = array[0] ;   //stores lines into array tokens 
     index++ ; 
     quarters[index] = array[1] ; 
     index++ ; 
    }  
     inFile.close() ;       // close connection to file 
     return quarters ; 
} // end information 

/** * (打印出所需的信息)* * @參數在數據陣列 拉動* @ param拉入數字正確的數組 * @param拉入百分比正確數組 * @param *(列出所有參數,每行一個) * @return無*/ public static void o utputput(tokens [],correct [],percentage [],letterGrade [],double totalAVG,int highScore,int lowScore) PrintWriter pw = new PrintWriter(new FileWriter(「Result.txt」));

for (int t=0 ; t< tokens.length ; t+=2) 
{ 
    g = 0 ; 
    pw.println(tokens[t] + "," + correct[g] + percentage[g] + letterGrade[g]) ; 
    g++ ; 
} 
    pw.println("Average: " + totalAVG + "% (" + totalGrade + ")") ; 
    pw.println("High Score: " + highScore*2) ; 
    pw.println("Low Score: " + lowScore*2) ; 

pw.close() ; 

} //結束輸出

} //結束類

+0

哦,它讀取的文件有行(從1-50的任何位置)格式化,像這樣4563123,112211324135412。 – 2013-03-17 23:59:45

+2

現在_that_s_牆的代碼! – Reimeus 2013-03-18 00:00:16

+2

他們看到我滾動,他們恨... – 2013-03-18 00:01:22

回答

0

林太短而爬上壁上。我會給你一個ArrayList的例子,但:

ArrayList<String> dinosaur = new ArrayList<String>(); // <String> is an explicit type declaration 


    // This will reference one line at a time 
    String line = null; 
    int i=0; 
    try 
    { 
     // FileReader reads text files in the default encoding. 
     FileReader fileReader = new FileReader("file.txt"); 

     //create buffered reader obj 
     BufferedReader bufferedReader = new BufferedReader(fileReader); 

//while not EOF, store line in array element 
     while ((line = bufferedReader.readLine()) != null) { 

     dinosaur.add(line); 

     i++; 
     } 


     // Always close files. 
     bufferedReader.close(); 
    } 
    //handle exceptions 
    catch(FileNotFoundException e) 
    { 
     System.out.println("Unable to open file" + e); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error reading file" + e); 
    } 

這將讀取文本文件中的每一行,並將其存儲在一個ArrayList的字符串(雖然列表是比較常用的首選)。 ArrayList是動態的,因此您不需要知道要存儲多少元素(文件中的行數)......只要堆棧中有足夠的內存來處理所有內容即可:p

Hope這有助於=)

0

這裏有一些方法:

1)是預先閱讀文件,計算有多少行有(例如使用掃描儀的HasNextLine(http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#hasNextLine()),然後就可以初始化數組要大一些並重新計數

2)是使用一個集合,如ArrayList<String>和在迭代文件時,只需添加行即可。 ArrayList會根據需要自動調整大小或條目數量,因此您無需事先擔心文件的行數。使用它在概念上與數組相同 - [i]適用於ArrayList罰款,以及addremove等方法,它們更改ArrayList中的條目數,而不是編輯已存在的條目數。

相關問題