找出有多少数组元素我从一个文件(不知道列表还)



我需要帮助使数组列表,以便我可以找出有多少元素的数组需要当我从文本文件中读取它们。我还没有学会如何做到这一点,并帮助将非常感激。我将显示需要修复的代码部分,因为我得到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(字符串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 */公共静态无效输出(tokens[], correct[], percentage[], letterGrade[], double totalAVG, int highScore, int lowScore){printwwriter pw = new printwwriter (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() ;

}//结束输出

}//结束class

我太矮了,爬不上那堵墙。我将给你一个数组列表的例子:

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

希望有帮助=)

以下是一些方法:

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中的条目数量,而不是编辑已经存在的条目。

相关内容

最新更新