I have a CSV file that I am reading from. I have created a constructor with six arguments for initialising the six fields and a public string method to convert a string into the format I need.
public Hill(int number, String name, String countyName, double height, double latitude, double longitude) {
this.number = number;
this.name = name;
this.countyName = countyName;
this.height = height;
this.latitude = latitude;
this.longitude = longitude;
}
Now what I am trying to achieve is after opening the file and reading it, it creates the corresponding object and then to return the list of the items.
I was given an inital method of public static List<Hill> readHills()
I have commented out a line which i tried to create a new object from simply because I wasn't sure if I was doing it right, so i just printed out as a test to see what my code was doing and it seems that the new object line is redundant. Currently my code is:
public static List<Hill> readHills() {
Scanner fileName = null;
try {
fileName = new Scanner(new File("C:/Users/TOSHIBA/Desktop/hills.csv"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner finalFileName = fileName;
List<Hill> list = new ArrayList<Hill>() {
{
for(int i = 0; i < 21; i++) {
String line = finalFileName.nextLine();
String[] splitted = line.split(",");
//add(new Hill(1, "", "", 100, 100, 100));
System.out.println(Arrays.toString(splitted));
}
}
};
return list;
}
In the code where I create the object I put a sample value but technically I want the 6 fields of the new object to be from the file I read. If i try adding the field names, i get a red line saying Non static method cannot be referenced from a static method
Where am i going wrong? And why is my line of code useless?
I continued trying to play with the code but can't get anywhere, I think my issue is not understand the method name i have been given; i.e. Why and what does public static List<Hill> readHills()
do?
I tried using Hill newHill = new Hill( int number, String name, String countyName,double height, double latitude, double longitude);
but can't since I cant reference a non-static field from a static context