我需要幫助製作一個數組列表,以便我可以找出數組中需要多少元素,當我從文本文件中讀取它們時。我還沒有學會如何做到這一點,幫助將不勝感激。如果我沒有正確存儲在數組中,我將顯示需要修復的代碼部分,因爲我得到空值。這篇文章是需要幫助的,但下面是完整的代碼。找出我從文件中得到多少個數組元素(不知道列表)
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() ;
} //結束輸出
} //結束類
哦,它讀取的文件有行(從1-50的任何位置)格式化,像這樣4563123,112211324135412。 – 2013-03-17 23:59:45
現在_that_s_牆的代碼! – Reimeus 2013-03-18 00:00:16
他們看到我滾動,他們恨... – 2013-03-18 00:01:22