Jacques Naudé
Jacques Naudé

Reputation: 40

Java: How to read only int data from text file to perform calculation?

I use a button to save user input to a text file and now i want to retrieve it to do a calculation. The text file is gets updated and new values are added. Here is an e.g of how the text file looks:

  1. monday : 1 tuesday: 2 wednessday: 3

...etc

When there is a new input it will be added, so the updated text file looks like this:

  1. monday : 1 tuesday: 2 wednessday: 3
  2. monday : 4 tuesday: 1 wednessday: 3
  3. monday : 6 tuesday: 5 wednessday: 6
  4. monday : 7 tuesday: 6 wednessday: 5

Basically i want to compare the new input with the previous input. How do I only retrieve all the integers from the latest and last input?

or should i rather use excel?

Here is my code:

try (
    InputStream fs = new FileInputStream("data.txt");
    // not sure how to use the Charset.forname
    InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8"));
    BufferedReader br = new BufferedReader(isr)) {
    for (String line1 = br.readLine(); line1 != null; line = br.readLine()) {
        comp = line1 - line2; //line1 being current line(4) and line2 being the previous(3)
    }
}

Upvotes: 1

Views: 269

Answers (2)

user1433620
user1433620

Reputation: 25

  1. need to be a bit more clear regarding insert/extract code, is it done by same process/thread. if the answer is yes i would store the requested data in a variable and this way you save some time accessing file and manipulating its data.

  2. in case the insert is done by another process/thread you should be carefuller, as there is a read/write race.

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 44995

Simply:

  1. Read your file by keeping only the 2 last lines,
  2. Then using a regular expression extract the integers.

The corresponding code:

try (
    InputStream fs = new FileInputStream("data.txt");
    InputStreamReader isr = new InputStreamReader(fs, StandardCharsets.UTF_8);
    BufferedReader br = new BufferedReader(isr)) {
    // Previous line
    String prev = null;
    // Last line
    String last = null;
    String line;
    while ((line = br.readLine()) != null) {
        prev = last;
        last = line;
    }
    // Pattern used to extract the integers
    Pattern pattern = Pattern.compile("\\d+");
    // Matcher for the previous line
    Matcher matcher1 = pattern.matcher(prev);
    // Matcher for the last line
    Matcher matcher2 = pattern.matcher(last);
    // Iterate as long as we have a match in both lines
    while (matcher1.find() && matcher2.find()) {
        // Value of previous line
        int val1 = Integer.valueOf(matcher1.group());
        // Value of last line
        int val2 = Integer.valueOf(matcher2.group());
        // Do something here
    }
}

NB: This assume that we have the exact same number of integers in both lines, otherwise you could compare unrelated values.


Another approach in case you use Java 8, you could use non integers as separator then rely on splitAsStream(CharSequence input) to extract all your integers as a List:

...
// Non integers as a separators
Pattern pattern = Pattern.compile("\\D+");
// List of extracted integers in previous line
List<Integer> previous = pattern.splitAsStream(prev)
    .filter(s -> !s.isEmpty())
    .map(Integer::valueOf)
    .collect(Collectors.toList());
// List of extracted integers in last line
List<Integer> current = pattern.splitAsStream(last)
    .filter(s -> !s.isEmpty())
    .map(Integer::valueOf)
    .collect(Collectors.toList());
// Do something here

Upvotes: 1

Related Questions