How do I compare each line of a text file? Java

I have a text file with content like 792:

der 17788648
und 14355959
die 10939606
Die 10480597

      

Now I want to compare if "Die" and "die" are equal in lower case. Thus, if the two lines in the bottom loop are equal, copy that word to a new text file on the bottom line and sum the values.

Expected Result:

der 17788648
und 14355959
die 114420203

      

I have so far:

    try {
        BufferedReader bk = null;
        BufferedWriter bw = null;

        bk = new BufferedReader(new FileReader("outagain.txt"));
        bw = new BufferedWriter(new FileWriter("outagain5.txt"));

        List<String> list = new ArrayList<>();
        String s = "";
        while (s != null) {
            s = bk.readLine();
            list.add(s);
        }


        for (int k = 0; k < 793; k++) {
            String u = bk.readLine();
            if (list.contains(u.toLowerCase())) {

                //sum values?

            } else {
                bw.write(u + "\n");
            }
        }

        System.out.println(list.size());

    } catch (Exception e) {
        System.out.println("Exception caught : " + e);
    }

      

+3


source to share


5 answers


Use HashMap to keep track of unique fields. Before you make a mark, try to find out if the value is already. If so, sum the old value with the new one and re-insert it (this replaces the old row with the same key)



package com.foundations.framework.concurrency;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class FileSummarizer {

  public static void main(String[] args) {
    HashMap<String, Long> rows = new HashMap<String, Long>();
    String line = "";
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader("data.txt"));
      while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(" ");
        String key = tokens[0].toLowerCase();
        Long current = Long.parseLong(tokens[1]);

        Long previous = rows.get(key);
        if(previous != null){
          current += previous;
        }
        rows.put(key, current);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      try {
        reader.close();
        Iterator<String> iterator = rows.keySet().iterator();
        while (iterator.hasNext()) {
          String key = iterator.next().toString();
          String value = rows.get(key).toString();

          System.out.println(key + " " + value);
        }
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

      

0


source


list.add(s);

Use instead list.add(s.toLowerCase());

. Right now, your code is comparing the lines of the undefined case with the lines below the trimmed ones.



+1


source


In Java 8, the best approach to common problems like reading files, comparing, grouping, collecting is to use the streams api, as it is much more concise. At least when the files are only a few kilobytes, then there will be no problem with this. Something like:

Map<String, Integer> nameSumMap = Files.lines(Paths.get("test.txt"))
            .map(x -> x.split(" "))
            .collect(Collectors.groupingBy(x -> x[0].toLowerCase(),
                    Collectors.summingInt(x -> Integer.parseInt(x[1]))
            ));

      

First you can read the file from Files.lines()

which returns Stream<String>

than you can split the strings into Stream<String[]>

, finally you can use functions groupingBy()

and summingInt()

to group by the first element of the array and sum the second.

If you don't want to use the Stream API, you can also create HashMap

and perform the summation manually in a loop.

+1


source


The String class has an equalIgnoreCase method that you can use to compare two strings regardless of case. So:

String var1 = "Die";
String var2 = "die";

System.out.println(var1.equalsIgnoreCase(var2));

      

Will output TRUE.

0


source


If I understand your question correctly, you want to know how you can get the prefix from a file, compare it, get the value behind it, and sum it for each prefix. It's right?

You can use regular expressions to get prefixes and values โ€‹โ€‹separately. Then you can sum all values โ€‹โ€‹with the same prefix and write them to a file for each one.

If you are not familiar with regular expressions, these links may help you:

Regex on tutorialpoint.com

Regex at vogella.com

For more tutorials, just scan google for "java regex" or similar tags.

If you don't want to differentiate between uppercase and lowercase strings, just convert them all to lower / uppercase before comparing them as @spork already explained.

0


source







All Articles