# Thread: Is it possible to make this in Java? Challenging question.

1. ## Equation problems in Java

y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

and continues until the maximum number in a text file.
"x0" is the first number in the text file and so on.
Here my text file name is "Xdata.txt" that contains:
1.2
-1.7
0.8
4.6
0.9
1.0
0.0
5.4
3.9
9.2
1.5
4.92
-0.5
3.7
2.5
8.0
9.8
-0.8
1.0
2.0

The program read from the "Xdata.txt" and get all the outputs into "Ydata.txt"
Last edited by matt_well; 07-27-2008 at 08:42 AM. Reason: Attached textfile  Reply With Quote

2. ## It reads a number (x0) from a file and does some computations on it until when? What stops the computation sequence? Your example shows it stopping at y10.
continues until the maximum number in a text file
The maximum number(by that I assume you mean the largest) that is in the file is 9.8. It's hard to do something 9.8 times. Do you mean read to end of file?

By doing algebraic substitution on the 10 equations you show, you can reduce it to a single equation. So why do you show 10 equations? Can there be more? How many?
It seems like the program only needs this simple form:
loop until eof
compute y10 = <the reduced equation>
write y10
end of loop

What is the program supposed to do?

It is not possible to write a program without there being much better specifications.
Last edited by Norm; 07-26-2008 at 07:55 PM.  Reply With Quote

3. ##  Originally Posted by Norm It reads a number (x0) from a file and does some computations on it until when? What stops the computation sequence? Your example shows it stopping at y10.
The maximum number(by that I assume you mean the largest) that is in the file is 9.8. It's hard to do something 9.8 times. Do you mean read to end of file?
No no, the idea is like this.
Maximum number I mean here is the numbers that the textfile have from beginning until the last one.
What I mean is like this, x0 is the first number in the text file until the last value in the text file.
My text file name is "Xdata.txt" contains X0 until the last X19, so there is maximum of 20 numbers.

x0 = 1.2
x1 = -1.7
x2 = 0.8
x3 = 4.6
x4 = 0.9
x5 = 1.0
until
X19 = 2.0

Here's my details explaination,

y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

and continues until the last number in a text file.  Reply With Quote

4. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
14

## Yes, of course it's possible, but, since I believe this is probably a homework assignment (and even if it's not) I won't do it for you. Try it yourself, and if you get stuck, post your code and all error/compiler messages and we will help you correct it, but we will not do it for you.  Reply With Quote

5. ##  Originally Posted by masijade Yes, of course it's possible, but, since I believe this is probably a homework assignment (and even if it's not) I won't do it for you. Try it yourself, and if you get stuck, post your code and all error/compiler messages and we will help you correct it, but we will not do it for you.
Hehe, it's not homework assignment, just learning Java for fun.
I hope to have assignment too.:D
I am a electrical graduated researcher now, just want to get to learn more on Java that I learn about some basic before during school.  Reply With Quote

6. ## This question is from my stupid idea mind to play with this textfile previously.
Ok, reading input from txt file is not the problem, but now the problem is that how should I start a for loop for these equation in this case using the continuous input x0 until x19 read from the textfile?

From the text file,
y0 = 1.2
y1 = 2*1.2 + (-1.7)
blah blah...

where...

y0 = x0
y1 = 2*y0 + x1

y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10

As you can see, the previously output are use for the next input, so I am thinking how's the for loop can be create.  Reply With Quote

7. ## Ok, I missed the x variable at the end of each equation.
So your problem is to read a line with a number, convert it to a number and if its the first line save the value in y and go read another line.
If its not the first line, compute a new value for y using the old value of y and the new value read from the line x.
Continue until end of file.
Java Code:
```  loop until eof:
read line & convert to x
if first line
y = value;
else
y = func(y, x, oldy)
save oldy
end loop```
You know this has NOTHING to do with java programming and you are wasting time trying to write this kind of program. Its purely an exercise in creating an algorithm for an obscure problem.
If you want to learn java, I'd suggest finding some more productive problems to solve. There are several sites you can Google that will have sample student exercises you can attempt.

Good luck.
Last edited by Norm; 07-26-2008 at 10:49 PM.  Reply With Quote

8. ## Thanks Norm for some guides. Here's the current doing, but cannot run yet.

Java Code:
```import java.io.*;
import java.util.*;

public class FindOutputs
{
public static void main(String[] args) throws java.io.IOException
{
String inputFileName = "Xdata.txt";

List<String> fileData = getData(inputFileName);

System.out.println('\n'+ "fileData size = " + fileData.size());

String path = "C:\\Ydata.txt";
PrintWriter pw = new PrintWriter(
new FileOutputStream(path));

int maxSize = fileData.size();

for(int i = 0; i < maxSize; i++)
{
String data = getNextValue(fileData, i);
double x = parseValue(data);
if(firstline = ){
y = value;
}

else{y = func(y, x, oldy);

pw.printf("%1.2f%n",oldy);
}
}
pw.close();
System.out.print( "Output file at C drive now" + '\n');
}

private static List<String> getData(String filePath) throws IOException
{
List<String> list = new ArrayList<String>();
String buffer;
while( (buffer = br.readLine()) != null)
{
}
br.close();
return list;
}

private static String getNextValue(List<String> list, int index)
{
if(index > list.size()-1) // gone past end of list
return "";
return list.get(index);
}

private static double parseValue(String s)
{
if(s.equals("")) // missing data values appear as empty string
return 0;
return Double.parseDouble(s);
}
}```

I am not so sure about the meaning..
Java Code:
```    if first line
y = value;
else
y = func(y, x, oldy)```
How to get the first line and how's the working inside the function?
I am on stuck in the middle part. Correct me.  Reply With Quote

9. ##   Reply With Quote

10. ## Still interest in doing, can you give me some guide please how this work?
Java Code:
```    if first line
y = value;
else
y = func(y, x, oldy)```  Reply With Quote

11. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
14

## Well, set a boolean true before you start reading, and after reading the first line set it false. Use that boolean to determine whether or not you've already read the first line.

That's one easy way to do it, though not necessarily the most sophisticated.  Reply With Quote

12. ## The 3rd and following equations you show can be replaced by a single one:

y[i] = 2*y[i-1] - y[i-2] + x[i]

You need to handle the cases where i is less than 2 manually, then this equation will work forever after that.  Reply With Quote

13. ## Hi, here's some doing ideas.

Java Code:
```        for(int i = 2; i < maxSize; i++)
{
String data = getNextValue(fileData, i);
double value = parseValue(data);
// What's the way to assign the 'double value' as x[i]?

if(x){

y = x ; //how to read the first value 'x' only?

}
else if(x){

y = 2*y + x; //how to read the value 'x' only?
}
else{

y[i] = 2*y[i-1] - y[i-2] + x[i];
}

pw.close();
System.out.print( "Output file at C drive now" + '\n');
}```
Guide and Correct me, see I may do some mistakes. Thanks.
Last edited by matt_well; 07-27-2008 at 07:46 PM.  Reply With Quote

14. ## to assign the 'double value' as x[i]?
Do you mean: x[i] = value; // save value in current x  Reply With Quote

15. ## As I know the 'value' is already converted to a 'double value'.
Is it like this? Then, how to declare the x[i] and y[i]?

Java Code:
```        for(int i = 2; i < maxSize; i++)
{
String data = getNextValue(fileData, i);
double value = parseValue(data);
x[i] = value;
......```  Reply With Quote

16. ## Are you asking how to create an array?

double[] x = new double; // assume < 100 nbrs in file
double[] y = new double;  Reply With Quote

17. ##  Originally Posted by Norm Are you asking how to create an array?

double[] x = new double; // assume < 100 nbrs in file
double[] y = new double;
How to change the 100 to something like "buffer" as in future if the number in the text file is "unknown" or "to be greater than that"?  Reply With Quote

18. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
14

## Use an ArrayList. See the API docs.  Reply With Quote

19. ## Is there a reason you need to save all the values of x and y?
What is supposed to be the output from the program?
You only need to save the last two values of y to compute the new y[i] value.  Reply With Quote

20. ##  Originally Posted by masijade Use an ArrayList. See the API docs.
Yes, I know that. How to use it here the double value?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•