# Thread: Solving this equations problem in Java

1. ## Solving this equations problem in Java

Previously messed up, so I explain here my problem more details.
The problem is like this:

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:

Java Code:
```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"

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

:)  Reply With Quote

2. ## First is saved by getting the x from the textfile and assigned it to y.
y = x
Second, getting the x from the textfile to calculate y.
y = 2*y + x

Then after that, can calculate the rest.
y[i] = 2*y[i-1] - y[i-2] + x[i]

The output should be the output 'y[i]' only.

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 = 2; i < maxSize; i++)
{
String data = getNextValue(fileData, i);
double value = parseValue(data);

if(x){

y = x ;

}
else if(x){

y = 2*y + x;
}
else{

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

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 run my program but having errors that possibly dealing with the array that I am thinking for one week already still unsolved:
FindOutputs.java:27: cannot find symbol
symbol : variable x
location: class FindOutputs
if(x){
^
FindOutputs.java:29: cannot find symbol
symbol : variable y
location: class FindOutputs
y = x ;
^
FindOutputs.java:29: cannot find symbol
symbol : variable x
location: class FindOutputs
y = x ;
^
FindOutputs.java:32: cannot find symbol
symbol : variable x
location: class FindOutputs
else if(x){
^
FindOutputs.java:34: cannot find symbol
symbol : variable y
location: class FindOutputs
y = 2*y + x;
^
FindOutputs.java:34: cannot find symbol
symbol : variable y
location: class FindOutputs
y = 2*y + x;
^
FindOutputs.java:34: cannot find symbol
symbol : variable x
location: class FindOutputs
y = 2*y + x;
^
FindOutputs.java:34: operator + cannot be applied to int,<any>
y = 2*y + x;
^
FindOutputs.java:38: cannot find symbol
symbol : variable y
location: class FindOutputs
y[i] = 2*y[i-1] - y[i-2] + x[i];
^
FindOutputs.java:38: cannot find symbol
symbol : variable y
location: class FindOutputs
y[i] = 2*y[i-1] - y[i-2] + x[i];
^
FindOutputs.java:38: cannot find symbol
symbol : variable y
location: class FindOutputs
y[i] = 2*y[i-1] - y[i-2] + x[i];
^
FindOutputs.java:38: cannot find symbol
symbol : variable x
location: class FindOutputs
y[i] = 2*y[i-1] - y[i-2] + x[i];
^
FindOutputs.java:38: operator + cannot be applied to int,<any>
y[i] = 2*y[i-1] - y[i-2] + x[i];
^
13 errors

Hope experts can guide me through. Thanks.  Reply With Quote

3. ## The compiler can't find the definitions for the array x[] or for the array y[]. You MUST define all variables that you use in the program.  Reply With Quote

4. ##  Originally Posted by Norm The compiler can't find the definitions for the array x[] or for the array y[]. You MUST define all variables that you use in the program.
How should I define them? Where to define?  Reply With Quote

5. ## What are you using them for?
Define them in the class so that all members can reference them.  Reply With Quote

6. ##  Originally Posted by matt_well How should I define them? Where to define?
Define them before use. That's good practice all the time. Actually in your code you can do it before the for loop.  Reply With Quote

7. ## Instead of using arrays, try something like the following:

Java Code:
```// Start
y = x = getInput()       //y0 = x0
oldy = y                 //oldy = y0
y = 2* y + getInput()    //y1 = 2 * y0 + x1
// loop thru the rest
while((x = getInput()) != EOF) { //x = x2, x3, x4, ...
tmp = y                        // y1, y2
y = 2*y - oldy + x             // y2 = 2*y1 - y0 + x2, y3 = 2*y2 - y1 + x3
oldy = temp                    // = y1
}```  Reply With Quote

8. Member Join Date
Jul 2008
Posts
19
Rep Power
0

## FindOutputs.java:27: cannot find symbol
symbol : variable x
location: class FindOutputs
First read this error message, it says that program can not find a variable named x, so that tells you that you haven't defined a variable with that name.

And according to your program, x is an array (I assume you are storing int values in that).

So must declare it before using it.

int[] x = new int[size];
int[] y = new int[size];

After this, you should set values into this array. Then you can use it for all your operations.
Last edited by thusa; 08-05-2008 at 06:42 AM.  Reply With Quote

9. ##  Originally Posted by Norm Instead of using arrays, try something like the following:

Java Code:
```// Start
y = x = getInput()       //y0 = x0
oldy = y                 //oldy = y0
y = 2* y + getInput()    //y1 = 2 * y0 + x1
// loop thru the rest
while((x = getInput()) != EOF) { //x = x2, x3, x4, ...
tmp = y                        // y1, y2
y = 2*y - oldy + x             // y2 = 2*y1 - y0 + x2, y3 = 2*y2 - y1 + x3
oldy = temp                    // = y1
}```
How's should the "getInput()" be?  Reply With Quote

10. ## getInput() should return the next value of x. If x is in an array, then it would return x[i++]. If x is from a file, the Scanner class could do it. It depends on where x is located.  Reply With Quote

11. ##  Originally Posted by Norm getInput() should return the next value of x. If x is in an array, then it would return x[i++]. If x is from a file, the Scanner class could do it. It depends on where x is located.
Emm.. I mean where is the "getInput()" from and how to apply them to the line below?
Java Code:
```            String data = getNextValue(fileData, i);
double value = parseValue(data);```
Because I would need to make it to read x0 ONLY to get y0.
Another step is to to read x1 ONLY and perform addition to get y1.
y0 = x0
y1 = 2*y0 + x1

I get messed up by the "for loop" currently. :confused:  Reply With Quote

12. ## where is the "getInput()" from
getInput() is a method that returns the next x. The location of the next x is up to the programmer to decide.
the first time getInput() was called it returns x0, the next time it returns x1, the next time x2, and so on and so on.
For example:
Java Code:
```int getInput() {
String data = getNextValue(fileData);
int value = parseValue(data);
return value;
}```  Reply With Quote

13. ##  Originally Posted by Norm getInput() is a method that returns the next x. The location of the next x is up to the programmer to decide.
the first time getInput() was called it returns x0, the next time it returns x1, the next time x2, and so on and so on.
For example:
Java Code:
```int getInput() {
String data = getNextValue(fileData);
int value = parseValue(data);
return value;
}```
Just to make sure do I need to use private static for this getInput() like other?  Reply With Quote

14. ## to make sure
To make sure of what?  Reply With Quote

15. ##  Originally Posted by Norm To make sure of what?
To make sure do I need to use private static for this getInput() ?
Can you let me know how to use the getInput() here?  Reply With Quote

16. ## you would use getInput() to have the String returned by the getNextValue(args) method converted to an int value returned to the caller.
Not sure what you are asking!!??

usage: int anInt = getInput();

If you are having problems using the above method. write a small, simple program that uses it to see how it works. Then come back here if you have problems.
Last edited by Norm; 08-27-2008 at 02:38 AM.  Reply With Quote

17. ##  Originally Posted by Norm you would use getInput() to have the String returned by the getNextValue(args) method converted to an int value returned to the caller.
Not sure what you are asking!!??

usage: int anInt = getInput();

If you are having problems using the above method. write a small, simple program that uses it to see how it works. Then come back here if you have problems.
Can you show me some example of the above method. I have difficulties that my basic for Java book having limited of such example.  Reply With Quote

18. ## I can't write a getInput() method unless I know where the input is to be read.
From a person at the console?
From a file?

Here's an uncompiled, untested method to read from the console:
Java Code:
```  public int getInt() {
try {
//            System.out.println(">" + s);  // SHOW WHAT WAS READ
int x = Integer.parseInt(s);
return x;     // return what was entered as an int
}
}catch(Exception ex){  //IF INVALID INPUT
ex.printStackTrace();
}
RETURN -1906; // ERROR VALUE
}```
Last edited by Norm; 08-30-2008 at 10:10 PM.  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
•