Results 1 to 15 of 15
  1. #1
    vickyp1 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Program structure problem

    This is my first post. If I've posted incorrectly can u tell me
    I am stuck on an assignment I am doing for Uni. Its a program structure problem, I know what to do but I am unsure where to do it. I want to read the contents of a comma delimited file that is pointed to from an array...Station[]
    I dont know where to put this next bit
    “class Station{ ...Name...
    }”
    ...
    I need to be able to access it from anywhere so that later I can use it.So I can do things like

    station[0].getName

    anywhere.
    Even inside private classes like this
    public NavigationUni(){ // constructor for the object
    private class Dist1Handler implements ActionListener{
    public void actionPerformed(ActionEvent e){

    Here's a watered down version of my program...
    Java Code:
    package Test;
    // IMPORT FILES
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import javax.swing.JFrame;
    
    public class Test extends JFrame{
    
        public static void main(String[] args) {
            Test any = new Test();
            // THIS NEXT BIT IS JUST OPENING A TEXT , DELIM FILE *******************
    
                    String fileName = "station_info.txt";
            int numStations = 16;
            double convString;
            try
            {
                FileReader fro = new FileReader( fileName );
                BufferedReader bro = new BufferedReader( fro );
           // declare String variable and prime the read
                String stringRead = bro.readLine( );
    
                // create the array of stations
               // Station[] station = new Station[numStations];
                Station[] stations = new Station[numStations];
                // ensure of no null pointers
                for(int i = 0; i < numStations; i++){// THE NEXT LINE
                    stations[i] = new Station();
                }
                String delimiter = ","; // info in each line is delimited by ,
                int stn_cnt = 0;
                String[] temp ; // to deal with delimiter
    
                //while( (stringRead = bro.readLine() ) != null )
                while (stringRead != null)
                {
                    temp = stringRead.split(delimiter);
    // populating the array with the lines of info from the file
                    stations[stn_cnt].setName(temp[1]);
                    stations[stn_cnt].setLine(temp[2]);
                    convString = Double.parseDouble(temp[2]);
                    stations[stn_cnt].setX(convString);
                    convString = Double.parseDouble(temp[3]);
                    stations[stn_cnt].setY(convString);
    
                    stringRead = bro.readLine( );  // read next line
                    stn_cnt++;
                    System.out.println(stations[stn_cnt]);
                } // end of while
    
                bro.close( ); // close file
    
    
            } // end of try
            catch (Exception e){// catch exception if any
                System.err.println("Error: " + e.getMessage());
            } // end of catch
    
        } // end of main
    
    /*
     * AN INTERFACE IS BUILT FOR THE END USER, THE FOLLOWING IS A WATERED DOWN VERSION
     * private JTextField user_dest_angleTF, user_dest_distanceTF;
     * private Dist1Handler dist1_tfHandler;
     *
     */
        public NavigationUni(){ // constructor for the object
            private class Dist1Handler implements ActionListener{
            public void actionPerformed(ActionEvent e){
    /*
     * ****************************************************************************
     * it wont let me do this......or it wont let me use it......
     *
     * Station closest = stations[0];
     *
     */
            }
        }
    } // end of class Test
    /*
     ********************'**********************************************************
     * I DON'T KNOW WHERE TO PUT THIS NEXT BIT
     *
     *   class Station {
            String name;
    
    // the constructor
            public Station(){
                name = "";
            }
    
            public Station(String stn){
                name = stn;
            } // end of public Station
    
        } // end of class Station
     */
    Last edited by JosAH; 12-29-2011 at 03:08 PM. Reason: Added [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    able to access it from anywhere
    Have the class that contains the array have getter and setter methods. Pass a reference to the containing class to any class that wants to use those methods to access the data.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Program structure problem

    Station[] stations = new Station[numStations];

    Because the above line is declared within a method, it becomes a local variable. Either at the end of the method copy the array stations to a class member or use a class member in the first place e.g.
    Java Code:
    public class MyClass {
    private Station[] stations;
    public void readStationFile() {
        ...
        stations = new Station[numStations];
        ...
    }
    //getter method
    public Station[] getStations { return stations; }
    }
    From then on whenever you need to access the array from another class you do this:
    MyClass myClassInstance = new MyClass();
    Station[] myStations = myClassInstance.getStations();

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    From then on whenever you need to access the array from another class you do this:
    MyClass myClassInstance = new MyClass();
    Station[] myStations = myClassInstance.getStations();
    Creating a new instance of the MyClass class with not get you to the stations array that was created in an earlier version of MyClass. You should probably create only one instance of MyClass, fill its stations array with data and then have the reference to that one instance of the MyClass object available to the classes that need to get to the stations array in that one instance.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Program structure problem

    You can add myClassInstance.readStationFile() inbetween. Or in the constructor.

    or make the method and the class member static and access the field statically.
    Last edited by ozzyman; 12-29-2011 at 03:03 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    Using static is not a good way out of the problem.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Program structure problem

    If the read method was static it could read and return to wherever it was needed to right? Like:
    public class StationHelper {
    public static Station[] getStations() {}
    }
    then read the file with StationHelper.getStations() when it needs to be read.
    Or is that bad practice?

  8. #8
    vickyp1 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Program structure problem

    OK, thankyou for your advice. I shall look into it. It is much appreciated.xx

  9. #9
    vickyp1 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Program structure problem

    Quote Originally Posted by Norm View Post
    Have the class that contains the array have getter and setter methods. Pass a reference to the containing class to any class that wants to use those methods to access the data.
    Hiya, Ive done the set and get methods inside the class Station [as follows].How do I give a reference in here to another class that wants this info? Sorry about this honestly. xx
    class Station {
    String name;

    public Station(){
    name = "";
    }

    public Station(String stn){
    name = stn;
    } // end of public Station

    public void setName(String stn){
    name = stn;
    } // end of setName

    public String getName(){
    return name;
    } // end of getName
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    How do I give a reference in here to another class that wants this info?
    That is a question about the overall design of the program.
    What classes need access to what information?
    Having a whole class to save one String seems like overdesign.

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Program structure problem

    You can see from the original post there are four setters being used in the station object. setName, setLine, setX, setY.


    To vickyp1, you need to create objects in order to be able to reference them. It would help if you showed us your attempt at referencing the Station objects so that we can see the way in which you need to reference them.

  12. #12
    vickyp1 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Program structure problem

    Quote Originally Posted by ozzyman View Post
    To vickyp1,... showed us your attempt at referencing the Station objects so that we can see the way in which you need to reference them.
    Ozzyman
    I apologise for my lack of understanding & experience-it really is back to basics with me!
    So if I do the following inside class Station{
    public String getName(){
    return name;
    }
    I would like to do things like
    stations[0].getName()
    outside of the method where it was set -public static void main(String[] args) {
    But when I try it, it says “stations” is a variable that it doesn’t know. So I don’t know how to access it or point to it.
    Thanks again, vicky xx

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    Where is the variable: stations defined? You will only be able to access that variable from places that its definition is known and in scope. In scope means within the same pair of {}s

  14. #14
    vickyp1 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Program structure problem

    Norm, stations is defined within
    public static void main(String[] args) {...}

    I was hoping to access it outside of that method. My watered down version of the program is at the beginning of the thread.
    Thanks ever so much for helping me xx

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Program structure problem

    stations is defined within ...
    Then it is not known outside of the main method. Look up what "in scope" means. Also see post#13
    You need to move the definition of the variable out of any methods. Define the variable at the class level.
    You can assign values to a variable that is defined outside of a method from within a method.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Problem using Structure in JNA
    By okaitt in forum New To Java
    Replies: 0
    Last Post: 04-28-2011, 03:59 PM
  3. Problem--How to create dynamic tree structure in jsp using javascript
    By natu in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-11-2010, 08:11 AM
  4. Replies: 1
    Last Post: 04-01-2010, 05:14 PM
  5. program structure
    By mac in forum New To Java
    Replies: 1
    Last Post: 12-30-2009, 11:59 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
  •