0

I have a text file like this:

Emma,F,20355
Olivia,F,19553
Sophia,F,17327
Ava,F,16286
Isabella,F,15504
Mia,F,14820
Abigail,F,12311
Emily,F,11727

I am trying to remove words after , and also put two lines in one line for every two lines. For example:

Emma Olivia
Sophia Ava
Isabella Mia
Abigail Emily

The program can do the first part, but I don't know how the program can do the second part. I can split the words and numbers after first ,, but I got stuck how I can can arrange lines.

Here is the code:

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
String[] a;
String res;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    res = a[0] + "\n";
    writer.write(res);
}
writer.close();
reader.close();

I think I need to create a for loop inside while loop, but I am not sure what to write to count even or odd lines.

4castle
  • 32,613
  • 11
  • 69
  • 106
Munchmallow
  • 311
  • 1
  • 2
  • 15

5 Answers5

2

Change to to something like this :

int count = 1;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    res = a[0] + count % 2 == 0 ? "\n" : " ";
    count++;
    writer.write(res);
}
4castle
  • 32,613
  • 11
  • 69
  • 106
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
2
try (
    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))
) {
    while (true) {
        String line1 = reader.readLine();
        if (line1 == null) { 
            break;
        }
        writer.write(line1.split(",", 2)[0]);

        String line2 = reader.readLine();
        if (line2 == null) {
            writer.newLine();
            break;
        }
        writer.write(" " + line2.split(",", 2)[0]);
        writer.newLine();
    }
}
4castle
  • 32,613
  • 11
  • 69
  • 106
1
int newLine = 1;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    if (newLine % 2 == 0)
        res += a[0] + "\n";
    else
        res += a[0] + " ";
    newLine++;
}
writer.write(res);
4castle
  • 32,613
  • 11
  • 69
  • 106
  • Storing an entire file in a string isn't a very good idea. It's better to write to the other file as you iterate. – 4castle Jun 17 '16 at 21:09
1

Try reading two lines in at the same time if there is a second line left in the reader.

Something like this:

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
String[] a;
String[] b;
String res;
while ((currentLine = reader.readLine()) != null) {
    a = currentLine.split(",");
    if (reader.hasNext()) {
        b = reader.readLine().split(",");
        res = a[0] + " " + b[0] + "\n";
    } else {
        res = a[0]+"\n";
    }
    writer.write(res);
}
writer.close();
reader.close();
4castle
  • 32,613
  • 11
  • 69
  • 106
Jeff Neet
  • 734
  • 1
  • 8
  • 22
  • 1
    While this looks *okay*, I would prefer [`try-with-resources`](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) to the two explicit `close` calls. – Elliott Frisch Jun 17 '16 at 20:25
  • @ElliottFrisch That's a good idea. Is my answer doing that right? – 4castle Jun 17 '16 at 20:38
  • That's a fair point. I simply copied the code provided by the OP and modified the parts directly relevant to their question. However, instantiating the BufferedReader within a try-with-resources block (assuming Java 7+) is definitely a safer way to go. – Jeff Neet Jun 17 '16 at 20:41
0

As mentioned above, read in two lines at a time. Combine them, then split based on the delimiter (comma) - Should then be easy to write out new format to a text file (Maybe pop the results in a list, then iterate over the list to write it out.

This is not a complete solution, but should be enough for you to get the idea.

// Read two lines at a time
String currentLine = reader.readLine(); //Emma,F,20355
String nextLine = reader.readLine();    //Olivia,F,19553

String combinedLine = currentLine + "," + nextLine;

// split into separate elements
String[] elements = combinedLine.split(",");

List<String> newLines = new ArrayList<>();
newLines.add(elements[0] + " " + elements[3]);

for (final String line : newLines) {
    // write to file
    writer.write(res);
}
4castle
  • 32,613
  • 11
  • 69
  • 106
MikeJ
  • 2,367
  • 3
  • 18
  • 23