简体   繁体   中英

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

The first group should contain odd lines and second should create even but its not working? (in java)

code:

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");
        }

    }
}

Change your buffered reader to :

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

I think the other solution don't do an copy but an reference streamed.


But I thinks the good way is to store the data into an two array with one loop and print the result after. Normaly you need only one buffer for read a file. And only one fileReader for one file :) Check your algo

Your BufferredReader 's share the same FileReader instance.

It seems that changing code to:

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

will work.

But it won't work. You need to reset counter to 0 before printing the second group:

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

One last thing: use counter % 2 == 0 to check if the counter value is even

And another last thing: you have massive code duplication here. It's better to create a method that'll print even or odd lines.

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");
    }
}

And then call this method from main :

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

It's cleaner.

You only need to read the file one time.

... 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM