Search code examples
javatransfer

Beginner assistance with transferring information between classes


I am working on a homework assignment that takes input from a .csv file and will prompt the user for different questions pertaining to the information contained within (crime statistics).

My code is as follows and it's still really early so I just have some placeholder variables in there as I have been wracking my head trying to figure out the best approach to this problem.

import java.io.*;

public class USCrimeArray {
String crimeArray[][] = new String[21][20];

    public void createCrimeArray() throws Exception{
        String crimeArrayInputString;
        int crimeArrayRowValue = -1;

        try (BufferedReader crimeArrayInput = new BufferedReader(new FileReader("C:/Users/Joey/Documents/Crime.csv"))) {
        while ((crimeArrayInputString = crimeArrayInput.readLine()) != null) {
            crimeArrayRowValue++;
            crimeArray[crimeArrayRowValue] = crimeArrayInputString.split(",");
        }
    } catch (IOException io) {
        io.getMessage();
    }
}

public USCrimeArray(){
        String[][] thisArray = crimeArray.clone();
}

public String[][] getCrimeArray(){
        return crimeArray.clone();
}
}

This is the code for my first class and if I do a deepToString inside of createCrimeArray I get the information back that I want. The constructor for USCrimeArray hasn't really been thought out yet my main question is how to write the information to the crimeArray[][] so that I can carry it back over to other classes.

Once again this test main hasn't been thought out too far because I am still struggling with why my method is not writing over the crimeArray[][] with the while loop and it is as follows:

import java.util.Arrays;

public class USCrimeClass { 
    public static void main(String[] args) {
    USCrimeArray crimeArray = new USCrimeArray();
    String[][] test = crimeArray.getCrimeArray();
    System.out.println(Arrays.deepToString(test));
    }
} 

I know there's a lot I'm doing wrong here, but this is the end result so far after having altered everything over and over again and not making any progress. The result of the system out in this is obviously just a 21x20 array of null elements. Any help would be greatly appreciated.


Solution

  • You need to call createCrimeArray() in USCrimeClass

    public class USCrimeClass { 
        public static void main(String[] args) {
            USCrimeArray crimeArray = new USCrimeArray();
            crimeArray.createCrimeArray();
            String[][] test = crimeArray.getCrimeArray();
            System.out.println(Arrays.deepToString(test));
        }
    } 
    

    Also,

    1. in the constructor of USCrimeArray you are clonning the array into a local variable thisArray but never use it. this is redundant and can be safely removed.

    2. in getCrimeArray() you are returning a clone of the array. this is not needed (unless you want to keep USCrimeArray immutable). you can just return the array itself

    Instance variables

    instance variables are non static class level variables (much like crimeArray). One can consider instance variables as serving two purposes:

    1. "details" of the problem domain of the class. For example Person class will have instance variables such as firstName and lastName that are details of one person.

    2. "configuration" variables holding information related to the technological environment and not pertaining to the problem domain of the class. For example, one sometimes might find a class with a boolean deleted instance variable that signifies a "soft deleted" instance that is not to be presented to the user or included in calculations. the purpose behind this is to support undo of deletion.

    so crimeArray is of category details of USCrimeArray. common best practice is to initialise instance variables in the class constructor, so by the time you finish creating a new instance, you have one that has full and valid details. So I would move all of the code of createCrimeArray() into the constructor.

    If you need to modify an instance variable after it was initialised, then a "setter" method can be used. these have a standardized signature: public void setCrimeArray(crimeArray[][]). having a standardized signature allows your class to be used by frameworks and libraries that add functionality. For example, storing the data in a relational database, sending/recieving the data over the internet, etc.

    Now, I see that the external input that is used to populate the array comes from a file. The way it is coded now, USCrimeArray can only read one specific file from predetermined file syatem location. a more flexible way would be for the class to receive the specification for external input as an argument:

    public USCrimeArray(String filename) {
        ... 
        try (BufferedReader crimeArrayInput = new BufferedReader(new FileReader(filename))) {
        ... 
    
    }
    

    now the same class can be used to process an array from different files. now you can even make the file name an argument of the java program:

    public class USCrimeClass { 
        public static void main(String[] args) {
            USCrimeArray crimeArray = new USCrimeArray(arg[0]);
            System.out.println(Arrays.deepToString(test));
        }
    } 
    

    now the same java program can process different files without need for recompile.