Subtraction in Java value shortage
Subtraction in Java value shortage
I encounter a difficult problem. I am looking for a suggestion how to approach in this problem. I have three field in my dataset. I want to perform a subtraction.The problem is like that.
Time(s) a x
1 0.1 0.2
2 0.4
3 0.6
4 0.7
5 0.2 0.9
I need to perform a subtraction from (a-x). But the method of subtraction is like that at time 1s a
has value 0.1. The operation will be (0.1-0.2) 1st iteration. 2nd iteration (0.1-0.4)
. 3rd iteration (0.1-0.6)
.4th iteration (0.1-0.7)
But in 2nd iteration it will be (0.2-0.9)
.
a
2nd iteration (0.1-0.4)
3rd iteration (0.1-0.6)
4th iteration (0.1-0.7)
(0.2-0.9)
This is my problem statement. I want to write down this code in Java
. I don't need Java code
. I can write it down myself. I need a suggestion how to proceed in this approach?. One thought is that creating array for each variable
. But then stuck on loop. How the loop iterated? It is clear array a is static until it get next value, which is available at Time 5s.
Java
I don't need Java code
creating array for each variable
a
How does the arrays look like? Can you be clear on your input?
– Raviteja Vutukuri
Jul 2 at 19:23
If the
a
and x
arrays are different sized, you can consider using nested loops.– StaticBeagle
Jul 2 at 19:24
a
x
What form does your input have? CSV file or what?
– lexicore
Jul 2 at 19:37
@lexicore text file.
– Ellena Mori
Jul 3 at 13:52
3 Answers
3
This will depend on how large is your input file:
If the dataset fits into memory load it as either 2 separate array or as one array of Row
objects with a
and x
as fields. After that it's simple iteration remembering what was the last row that contained a
to use it when a
is missing.
Row
a
x
a
a
If the dataset is large it's better to read it using BufferedReader
and only remember the last encountered a
and x
. This will greatly reduce the memory consumption and would be the preferred approach.
BufferedReader
a
x
My data file has 100000 data.
– Ellena Mori
Jul 3 at 13:53
If a
changes every 4 numbers you can use time's / 4 + 1
to get value from small array of a
.
a
time's / 4 + 1
a
If a
changes not every 4 numbers, then I suggest to use full array filled with same values.
a
No my time does not change in every 4 number. It changes arbitrarily. Some time in 4 number some time 10 number etc. What do you mean by
full array filled with same values.
Did you mean to full array a with 0.0 values?– Ellena Mori
Jul 3 at 13:57
full array filled with same values.
Now that I see you're not using a database and just reading from a file, maybe try this
Just keep the old value of a
until a new value can overwrite it.
This is memory efficient since it parses line by line.
a
public static List<Double> parseFile(String myFile) throws IOException {
List<Double> results = new ArrayList<>();
try (BufferedReader b = new BufferedReader(new FileReader(myFile));) {
b.readLine(); // ** skip header?
String line;
Integer time = null;
Double a = null;
Double x = null;
for (int lineNum = 0; (line = b.readLine()) != null; lineNum++) {
// ** split the data on any-and-all-whitespace
final String data = line.split("s+");
if (data.length != 3)
throw new RuntimeException("Invalid data format on line " + lineNum);
try {
time = Integer.valueOf(data[0]);
if (!data[1].trim().isEmpty()) {
a = Double.valueOf(data[1]);
}
if (!data[2].trim().isEmpty()) {
x = Double.valueOf(data[2]);
}
} catch (Exception e) {
throw new RuntimeException("Couldn't parse line " + lineNum, e);
}
if (a == null || x == null) {
throw new RuntimeException("Values not initialized at line " + lineNum);
}
results.add(Double.valueOf(a.doubleValue() - x.doubleValue()));
}
}
// ** finished parsing file, return results
return results;
}
getAatTime
and getXatTime
Can you write down this two function. This two are not very clear to me. Yes a is null at the blank spaces.– Ellena Mori
Jul 3 at 14:15
getAatTime
getXatTime
@EllenaMori I have updated my answer now that I know this is just being parsed from a file.
– xtratic
Jul 3 at 16:53
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This isn't a very difficult problem to find a solution for, the difficult part is more selecting the one that is best. To get you started, for
a
you can create an array that is filled with the same value until it is changed so that you get two equally sized arrays.– Joakim Danielson
Jul 2 at 19:19