簡體   English   中英

我正在嘗試讀取文本文件,然后拆分行,以便獲得兩組不同的名稱

[英]I'm trying to read a text file and then split the lines so that i get two different sets of names

第一組應包含奇數行,第二組應創建偶數行,但不起作用? (在Java中)

碼:

import java.io.*;

class InArray2 {

    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("names.txt");
            BufferedReader br = new BufferedReader(fr);
            BufferedReader gr = new BufferedReader(fr);
            String register1;
            String register2;
            int counter = 0;
            System.out.println("Coursework A will be done by :");
            while ((register1 = br.readLine()) != null ) {
            counter++;
            if (counter == 1 || counter == 3 || counter == 5 || counter == 7 || counter == 9 ) 
               System.out.println(register1); 
            }

            System.out.println("Coursework B will be done by :");
            while ((register2 = gr.readLine()) ! = null ) {
            counter++;
            if (counter == 2 || counter == 4 || counter == 6 || counter == 8 || counter == 10 )
                    System.out.println(register2);
        }


            br.close();
        } catch (IOException e) {
            System.out.println("file not found");
        }

    }
}

將緩沖的閱讀器更改為:

   BufferedReader br = new BufferedReader(new FileReader("names.txt"));
   BufferedReader gr = new BufferedReader(new FileReader("names.txt"));

我認為其他解決方案不復制,而是流式傳輸參考。


但是我認為一個好方法是將數據存儲到一個循環的兩個數組中,然后打印結果。 通常,您只需要一個緩沖區即可讀取文件。 一個文件只有一個fileReader :)檢查您的算法

您的BufferredReader共享相同的FileReader實例。

似乎將代碼更改為:

    FileReader fr = new FileReader("names.txt");
    FileReader fr1 = new FileReader("names.txt");
    BufferedReader br = new BufferedReader(fr);
    BufferedReader gr = new BufferedReader(fr1);

將工作。

但這是行不通的。 您需要在打印第二組之前將計數器重置為0:

counter = 0;
System.out.println("Coursework B will be done by :");

最后一件事:使用counter % 2 == 0檢查計數器值是否為偶數

最后一件事:您在這里有大量的代碼重復。 最好創建一個可以打印偶數行或奇數行的方法。

public static void printOnlyModulo(int modulo, String header) {
    System.out.println(header);
    try {
        int counter = 0;
        BufferedReader reader = new BufferedReader(new FileReader("names.txt"));
        String line;
        while ((line = reader.readLine()) != null) {
            counter++;
            if (counter % 2 == modulo) {
                System.out.println(line);
            }
        }
        reader.close();
    }
    catch (IOException e) {
        System.out.println("file not found");
    }
}

然后從main調用此方法:

public static void main(String[] args) {
    printOnlyModulo(1, "Coursework A will be done by :");
    printOnlyModulo(0, "Coursework B will be done by :");
}

比較干凈

您只需要讀取一次文件。

... blah.
int lineNumber = 1;
List<String> theAList = new LinkedList<String>();
List<String> theBList = new LinkedList<String>();
while ((line = reader.readLine()) != null)
{
    if (lineNumber % 2) // even are on the b list.
    {
        theBList.add(line);
    }
    else // odd are on the a list.
    {
        theAList.add(line);
    }
}

System.out.println("A list");
for (String current : theAList)
{
    System.out.println(current);
}

System.out.println("B list");
for String current : theBList)
{
    System.out.println(current);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM