i am trying to read comma separated values from a text file which contains a postcode, latitude and longitude on each line and i want to parse this data and then access it when a user inputs a postcode to then find the lat and long on its corresponding line, which then the program takes the lat and long and finds the location on a map using the google maps api, however im having trouble.
this is where i read the file
public void readFromFile() {
BufferedReader read = null;
String line;
String fileName = "";
String seperator = ",";
try {
FileInputStream fstream = new FileInputStream("test.txt");
read = new BufferedReader(new InputStreamReader(fstream));
//read = new BufferedReader(new FileReader(fileName));
while ((line = read.readLine()) != null) {
postcodeCSV = line.split(seperator);
postcode = postcodeCSV[0] ;
line = read.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (read != null) read.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
This is my main file
public String postcode ;
public float latitude ;
public float longitude ;
public String[] postcodeCSV;
EditText POSTCODE_et;
ImageButton FIND_ib;
private LatLng ADDRESS = new LatLng(latitude, longitude);
private GoogleMap MAP_GM;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
POSTCODE_et = (EditText) findViewById(R.id.postcode_et);
FIND_ib = (ImageButton) findViewById(R.id.find_ib);
MAP_GM = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_gm)).getMap();
readFromFile();
FIND_ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (String s : postcodeCSV) {
String Postcode_getter = POSTCODE_et.getText().toString();
if (s == Postcode_getter) {
latitude = Float.parseFloat(postcodeCSV[1]);//postcodeCSV[1];
longitude = Float.parseFloat(postcodeCSV[2]);
}
CameraUpdate updateMap = CameraUpdateFactory.newLatLng(ADDRESS);
MAP_GM.animateCamera(updateMap);
}
}
});
}
I think what you are looking for is a
Map<String, LatLon> postCodeMap = new HashMap<>();
Where the Key is your postcode as a String and Value is a custom class LatLon as shown below:
public class LatLon{
double latitude;
double longitude;
//getter & setters
//constructor
}
To put a new Value
postCodeMap.put("55555", new LatLon(92.00,-32.75))
To Look Up a new Value:
LatLon latlon = postCodeMap.get("55555");
I think your problem stems from you overwriting the postcode and postcodeCSV for every other line. The every other line part comes from you doing while(line = read.readLine()) and then 5 lines later calling line = read.readLine() again.
Here is what I would do:
A) Create a class called PostOffice with code, lat and long as the fields.
B) remove the second readLine()
C) For each line create a new postOffice
D) Add each new postOffice to a List of postOffices (which should be a field)
F) In your onClick, iterate through the postOffices and check for the user entered value such as
for(PostOffice po : postOffices)
{
if(po.code == POSTCODE_et.getText().toString())
{
latitude = po.lat;
longitude = po.lon;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.