-2

I'm trying to solve a calculation problem in Java.

Suppose my data looks as follows:

466,2.0762
468,2.0799
470,2.083
472,2.0863
474,2.09
476,2.0939
478,2.098

It's a list of ordered pairs, in the form of [int],[double]. Each line in my file contains one pair. The file can contain seven to seven thousand of those lines, all of them formatted as plain text.

Each [int] must be subtracted from the [int] one line above and the result written onto another file. The same calculation must be done for every [double]. For example, in the data reported above, the calculation should be:

478-476 -> result to file
476-474 -> result to file
(...)
2.098-2.0939 -> result to file
2.0939-2.09 -> result to file

and so on.

I beg your pardon if this question will look trivial for the vast majority of you, but after weeks trying to solve it, I got nowhere. I also had troubles finding something even remotely similar on this board! Any help will be appreciated.

Thanks!

Federico
  • 11
  • 1
  • 1
    1). Create a class to represent the pair. 2) Create a collection, and read each pair, save in collection. 3) Traverse collection in reverse order, and subtract the values from current and prior element, write to file. If you really haves spent weeks on this, I would recommend reading The Java Tutorials sections on I/O and Collections. Why do you think this needs recursion to implement? – OldProgrammer Jul 25 '16 at 14:52

1 Answers1

1
  1. Read the file
  2. Build the result
  3. Write to a file

For the 1. task there are already several good answers here, for example try this one: Reading a plain text file in Java.

You see, we are able to read a file line per line. You may build a List<String> by that which contains the lines of your file.

To the 2. task. Let's iterate through all lines and build the result, again a List<String>.

List<String> inputLines = ...

List<String> outputLines = new LinkedList<String>();
int lastInt = 0;
int lastDouble = 0;
boolean firstValue = true;
for (String line : inputLines) {
    // Split by ",", then values[0] is the integer and values[1] the double
    String[] values = line.split(",");
    int currentInt = Integer.parseInt(values[0]);
    double currentDouble = Double.parseDouble(values[1]);

    if (firstValue) {
        // Nothing to compare to on the first run
        firstValue = false;
    } else {
        // Compare to last values and build the result
        int diffInt = lastInt - currentInt;
        double diffDouble = lastDouble - currentDouble;

        String outputLine = diffInt + "," + diffDouble;
        outputLines.add(outputLine);
    }

    // Current values become last values
    lastInt = currentInt;
    lastDouble = currentDouble;
}

For the 3. task there are again good solutions on SO. You need to iterate through outputLines and save each line in a file: How to create a file and write to a file in Java?

Community
  • 1
  • 1
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • This question is not a good question as per SO standards. The problem is not well defined, and the question only asks for the codez. The proper way to deal with it would be to ask for a specific problem, and the code that was created to try to solve the problem. We should not feed such a question with a complete solution only because the user requested it. – Laf Jul 25 '16 at 15:04
  • 1
    This may be true but that's no reason for a `-1` as the quality of the answer itself is not affected by this fact. – Zabuzard Jul 25 '16 at 15:15