Results 1 to 18 of 18
  1. #1
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default 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

    :)
    Attached Files Attached Files

  2. #2
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  4. #4
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What are you using them for?
    Define them in the class so that all members can reference them.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by matt_well View Post
    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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
    }

  8. #8
    thusa is offline Member
    Join Date
    Jul 2008
    Posts
    19
    Rep Power
    0

    Post

    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.

  9. #9
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  11. #11
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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:

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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;
     }

  13. #13
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    to make sure
    To make sure of what?

  15. #15
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  17. #17
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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 10:10 PM.

Similar Threads

  1. Suggestions required for solving a Java problem
    By bilal_ali_java in forum Advanced Java
    Replies: 3
    Last Post: 08-16-2008, 02:11 AM
  2. Problem in java
    By saytri in forum New To Java
    Replies: 4
    Last Post: 01-16-2008, 11:09 PM
  3. I need a help in solving this method using vectors
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 08:51 PM
  4. equations
    By Peter in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 07:15 AM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 11:10 AM

Posting Permissions

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