:Above is snipshot of csv file public class Hill { int number; String name; Stri
ID: 3823464 • Letter: #
Question
:Above is snipshot of csv file
public class Hill {
int number;
String name;
String CountyName;
double height;
double latitude,longitude;
Hill(int n,String name,String cname,double h,double lat,double longitude)
{
this.number=n;
this.name=name;
CountyName=cname;
height=h;
latitude=lat;
this.longitude=longitude;
}
}
Explanation / Answer
PROGRAM CODE:
package util;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Hill {
int number;
String name;
String CountyName;
double height;
double latitude,longitude;
Hill(int n,String name,String cname,double h,double lat,double longitude)
{
this.number=n;
this.name=name;
CountyName=cname;
height=h;
latitude=lat;
this.longitude=longitude;
}
public static List<Hill> readHills()
{
List<Hill> hills = new ArrayList<>();
try {
Scanner reader = new Scanner(new File("hills.csv"));
String line = reader.nextLine();
String tokens[] = line.split(",");
Hill hill = new Hill(Integer.valueOf(tokens[0]), tokens[1], tokens[2], Double.valueOf(tokens[3]), Double.valueOf(tokens[4]), Double.valueOf(tokens[5]));
hills.add(hill);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hills;
}
public static void exercise5b()
{
List<Hill> listOfHills = readHills();
for(int i=0; i<listOfHills.size()||i<20; i++)
{
System.out.print(listOfHills.get(i).number + " " + listOfHills.get(i).name + " " + listOfHills.get(i).CountyName);
System.out.println(listOfHills.get(i).height + " " + listOfHills.get(i).latitude + " " + listOfHills.get(i).longitude);
}
}
public static void main(String args[])
{
exercise5b();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.