Results 1 to 18 of 18
- 08-03-2008, 10:50 PM #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:
The program read from the "Xdata.txt" and get all the outputs into "Ydata.txt"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
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, 10:59 PM #2
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.
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[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:
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-03-2008, 11:24 PM #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.
- 08-03-2008, 11:40 PM #4
- 08-04-2008, 02:22 AM #5
What are you using them for?
Define them in the class so that all members can reference them.
- 08-04-2008, 03:42 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 08-04-2008, 03:53 PM #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 }
- 08-05-2008, 05:40 AM #8
Member
- Join Date
- Jul 2008
- Posts
- 19
- Rep Power
- 0
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.FindOutputs.java:27: cannot find symbol
symbol : variable x
location: class FindOutputs
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 05:42 AM.
- 08-09-2008, 04:31 PM #9
- 08-09-2008, 06:10 PM #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.
- 08-11-2008, 02:56 PM #11
Emm.. I mean where is the "getInput()" from and how to apply them to the line below?
Because I would need to make it to read x0 ONLY to get y0.Java Code:String data = getNextValue(fileData, i); double value = parseValue(data);
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, 12:13 AM #12
getInput() is a method that returns the next x. The location of the next x is up to the programmer to decide.where is the "getInput()" from
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; }
- 08-25-2008, 09:34 AM #13
- 08-25-2008, 01:54 PM #14
To make sure of what?to make sure
- 08-27-2008, 12:04 AM #15
- 08-27-2008, 01:26 AM #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 01:38 AM.
- 08-30-2008, 06:21 PM #17
- 08-30-2008, 09:05 PM #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 { 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 }Last edited by Norm; 08-30-2008 at 09:10 PM.
Similar Threads
-
Suggestions required for solving a Java problem
By bilal_ali_java in forum Advanced JavaReplies: 3Last Post: 08-16-2008, 01:11 AM -
Problem in java
By saytri in forum New To JavaReplies: 4Last Post: 01-16-2008, 10:09 PM -
I need a help in solving this method using vectors
By java_fun2007 in forum New To JavaReplies: 2Last Post: 11-26-2007, 07:51 PM -
equations
By Peter in forum New To JavaReplies: 2Last Post: 07-04-2007, 06:15 AM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks