写入后,Array不会保留其值

时间:2018-04-28 08:59:22

标签: java arrays

我正在尝试将文件读入数组,每行将存储在数组的索引中。给出text.txt文件中的以下输入:

6afe
5af
3eafe
7fae
3sfs
1eef

我读入数组,然后在控制台中打印出数组进行检查,但不知何故,数组在循环后不会保留其值。我得到的输出是null,在前后,但不在循环的中间。请告诉我为什么 ?谢谢 这是我得到的输出

nullline 0
nullline four
6afe
5af
3eafe
7fae
3sfs
1eef
nullline three
nullline four

我想要获得的输出是:

nullline 0             // array still empty here, i get it
    nullline four     // array still empty
    6afe         
    5af
    3eafe
    7fae
    3sfs
    1eef
    7fae      // where here are null three ? in the actual ouput
    3sfs      // null three ?

这是我的代码:

import java.io.*;

public class readSortWrite
{
    public static void main(String[] args) throws java.io.IOException
    {
        //input file
        // to count lineNum
        FileReader fr = new FileReader("text.txt");
        BufferedReader br = new BufferedReader(fr);
        //to read into array names[]
        FileReader fr1 = new FileReader("text.txt");
        BufferedReader br1 = new BufferedReader(fr1);

        // output file
        FileWriter fw = new FileWriter("sorted.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);

        // counting number of line
        int lineNum = 0;
        String line;
        line = br.readLine();
        while(line != null)
        {
            lineNum++;
            line = br.readLine();            
        }
        pw.close();
       // System.out.println(lineNum + "lines");  

        // create an array of of lineNum size and write each line of file into array
        String[] names = new String[lineNum];
        String str;
        str = br1.readLine();

        System.out.println(names[0]+ " line 0");
        System.out.println(names[4] + " line four");

        for (int i = 0 ; i<lineNum; i++)
        {
            while (str!= null)
            {
                names[i] = str;
                str = br1.readLine();
                System.out.println(names[i]);


            }             
        }
       // fr1.close();
        System.out.println(names[3]+ " line three");
        System.out.println(names[4] + " line four");



    }
}

5 个答案:

答案 0 :(得分:1)

前两行是null因为您创建数组并在初始化之前在04索引处打印值,就像这样

String[] names = new String[lineNum];
String str;
str = br1.readLine();

System.out.println(names[0]+ " line 0");
System.out.println(names[4] + " line four");

对于倒数第二行,while循环将读取buffer,直到它为空,但没有for循环的任何迭代,因此只填充names[0]

for (int i = 0 ; i<lineNum; i++)
{
  while (str!= null)
  {
    names[i] = str;
    str = br1.readLine();
    System.out.println(names[i]); 
  }             
}

将其更改为

int i =0;
while (str!= null && i < lineNum) {
  names[i] = str;
  str = br1.readLine();
  System.out.println(names[i]);
}  

答案 1 :(得分:1)

错误在于用于将文件读入数组的嵌套循环

    for (int i = 0 ; i<lineNum; i++)
    {
        while (str!= null)
        {
            names[i] = str;
            str = br1.readLine();
            System.out.println(names[i]);


        }             
    }

因此,对于i的第一个值,您需要阅读整个输入文件。当您使用嵌套的while循环时,整个文件已被读取,但索引保持不变。

yuo应该有一个循环读取文件,索引在每次迭代时前进

    int i = 0;
    while (str!= null)
    {
        names[i] = str;
        i++;
        str = br1.readLine();
        System.out.println(names[i]);
    }             

答案 2 :(得分:0)

您声明并实例化名称:

 String[] names = new String[lineNum];

但在此之后,当您尝试打印时,您无法将任何内容放入名称中:

System.out.println(names[0]+ " line 0");
System.out.println(names[4] + " line four");

在随后的for循环中,您将重复覆盖名称[0]。 while循环使用0作为i的值执行。

答案 3 :(得分:0)

只需将其替换为下面的代码即可。你正在使用一段时间真的

    String str;

    System.out.println(names[0]+ " line 0");
    System.out.println(names[4] + " line four");

    for (int i = 0 ; i<lineNum; i++)
    {
        str = br1.readLine();

        if (str!= null)
        {
            names[i] = str;
            System.out.println(names[i]);
        }
    }

答案 4 :(得分:0)

String[] names = new String[lineNum];
        String str;
        str = br1.readLine();

        System.out.println(names[0]+ " line 0");
        System.out.println(names[4] + " line four");

您刚创建名称[]的代码,因此输出为空。

for (int i = 0 ; i<lineNum; i++)
        {
            while (str!= null)
            {
                names[i] = str;
                str = br1.readLine();
                System.out.println(names[i]);


            }             
        }

代码,当i = 0时,你输入while而不是break,所以System.out.println(names [i]);始终输出名称[0]直到最后一个str。 和names []其他元素始终为null。