我需要帮助制作一个数组列表,以便在我从文本文件中读取数组时找出数组需要多少个元素。我还没有学会如何做到这一点,我们将不胜感激。如果我没有正确存储在数组中,我将显示需要修复的代码部分,因为我得到 null。这篇文章需要帮助,但下面是完整的代码。
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) ;
/* * * 打开到带有 id 和答案的文件的连接,并将它们分开返回。* @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 拉入百分比正确数组 * @param * (列出所有参数,每行一个) * @ return None */ public static void output(tokens[], correct[], percent[], 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() ;
} // 结束输出
} // 结束类