# Solving this equations problem in Java

• 08-03-2008, 11:50 PM
matt_well
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:

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

:)
• 08-03-2008, 11:59 PM
matt_well
First is saved by getting the x[0] from the textfile and assigned it to y[0].
y[0] = x[0]
Second, getting the x[1] from the textfile to calculate y[1].
y[1] = 2*y[0] + x[1]

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.

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[0]){                                 y[0] = x[0] ;                         }                         else if(x[1]){                                 y[1] = 2*y[0] + x[1];                         }                         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>();         FileReader fr = new FileReader(filePath);         BufferedReader br = new BufferedReader(fr);         String buffer;         while( (buffer = br.readLine()) != null)         {             list.add(buffer);         }         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:
Quote:

FindOutputs.java:27: cannot find symbol
symbol : variable x
location: class FindOutputs
if(x[0]){
^
FindOutputs.java:29: cannot find symbol
symbol : variable y
location: class FindOutputs
y[0] = x[0] ;
^
FindOutputs.java:29: cannot find symbol
symbol : variable x
location: class FindOutputs
y[0] = x[0] ;
^
FindOutputs.java:32: cannot find symbol
symbol : variable x
location: class FindOutputs
else if(x[1]){
^
FindOutputs.java:34: cannot find symbol
symbol : variable y
location: class FindOutputs
y[1] = 2*y[0] + x[1];
^
FindOutputs.java:34: cannot find symbol
symbol : variable y
location: class FindOutputs
y[1] = 2*y[0] + x[1];
^
FindOutputs.java:34: cannot find symbol
symbol : variable x
location: class FindOutputs
y[1] = 2*y[0] + x[1];
^
FindOutputs.java:34: operator + cannot be applied to int,<any>
y[1] = 2*y[0] + x[1];
^
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.
• 08-04-2008, 12:24 AM
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.
• 08-04-2008, 12:40 AM
matt_well
Quote:

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?
• 08-04-2008, 03:22 AM
Norm
What are you using them for?
Define them in the class so that all members can reference them.
• 08-04-2008, 04:42 AM
Eranga
Quote:

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.
• 08-04-2008, 04:53 PM
Norm
Instead of using arrays, try something like the following:

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 }```
• 08-05-2008, 06:40 AM
thusa
Quote:

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.
• 08-09-2008, 05:31 PM
matt_well
Quote:

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

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?
• 08-09-2008, 07:10 PM
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.
• 08-11-2008, 03:56 PM
matt_well
Quote:

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?
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:
• 08-12-2008, 01:13 AM
Norm
Quote:

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:
Code:

```int getInput() {             String data = getNextValue(fileData);             int value = parseValue(data);             return value;  }```
• 08-25-2008, 10:34 AM
matt_well
Quote:

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:
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?
• 08-25-2008, 02:54 PM
Norm
Quote:

to make sure
To make sure of what?
• 08-27-2008, 01:04 AM
matt_well
Quote:

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?
• 08-27-2008, 02:26 AM
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.
• 08-30-2008, 07:21 PM
matt_well
Quote:

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.
• 08-30-2008, 10:05 PM
Norm
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:
Code:

```  public int getInt() {         try {           BufferedReader br =new BufferedReader(                                         new InputStreamReader(System.in));             String s = br.readLine();    // read from the console //            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 }```