Skip to content

Commit 03b9f25

Browse files
TripViewActivity: Catch NumberFormatException
Signed-off-by: Keith Conger <keith.conger@blackboxembedded.com>
1 parent 6047657 commit 03b9f25

2 files changed

Lines changed: 109 additions & 59 deletions

File tree

app/src/main/java/com/blackboxembedded/WunderLINQ/AAuto/AAutoScreen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.ArrayList;
5656
import java.util.HashMap;
5757
import java.util.Map;
58+
import java.util.Objects;
5859

5960
public class AAutoScreen extends Screen {
6061

@@ -218,7 +219,7 @@ public GridItem getCellData(int dataPoint){
218219
String dataVal = (String) retObj[0];
219220
String label = (String) retObj[1];
220221
Drawable icon = (Drawable) retObj[2];
221-
IconCompat iconCompat = IconCompat.createWithBitmap(Utils.drawableToBitmap(icon));
222+
IconCompat iconCompat = IconCompat.createWithBitmap(Objects.requireNonNull(Utils.drawableToBitmap(icon)));
222223
CarIcon carIcon = new CarIcon.Builder(iconCompat).build();
223224

224225
return new GridItem.Builder()

app/src/main/java/com/blackboxembedded/WunderLINQ/TripViewActivity.java

Lines changed: 107 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class TripViewActivity extends AppCompatActivity implements OnMapReadyCal
8686
private PopupMenu mPopupMenu;
8787
private EditText etLabel;
8888
private List<LatLng> routePoints;
89-
private ArrayList tripFileList = new ArrayList<String>();
89+
private final ArrayList<String> tripFileList = new ArrayList<String>();
9090
private String fileName;
9191
private File file;
9292
private int index;
@@ -211,77 +211,126 @@ public void onSwipeRight() {
211211
e.printStackTrace();
212212
}
213213
if((lineNumber > 1) && (!nextLine[1].equals("No Fix") && (!nextLine[2].equals("No Fix")))) {
214-
LatLng location = new LatLng(Double.parseDouble(nextLine[1]), Double.parseDouble(nextLine[2]));
215-
if(lastLocation == null){
216-
lastLocation = new LatLng(Double.parseDouble(nextLine[1]), Double.parseDouble(nextLine[2]));
217-
} else {
218-
float[] results = new float[1];
219-
Location.distanceBetween(lastLocation.latitude, lastLocation.longitude,
220-
location.latitude, location.longitude, results);
214+
try {
215+
LatLng location = new LatLng(Double.parseDouble(nextLine[1]), Double.parseDouble(nextLine[2]));
216+
if(lastLocation == null){
217+
lastLocation = new LatLng(Double.parseDouble(nextLine[1]), Double.parseDouble(nextLine[2]));
218+
} else {
219+
float[] results = new float[1];
220+
Location.distanceBetween(lastLocation.latitude, lastLocation.longitude,
221+
location.latitude, location.longitude, results);
222+
223+
// Update total distance
224+
totalDistance += results[0];
225+
}
221226

222-
// Update total distance
223-
totalDistance += results[0];
227+
routePoints.add(location);
228+
speeds.add(Double.parseDouble(nextLine[4]));
229+
if (maxSpeed == null || maxSpeed < Double.parseDouble(nextLine[4])){
230+
maxSpeed = Double.parseDouble(nextLine[4]);
231+
}
232+
} catch (NumberFormatException e) {
233+
// Handle the invalid input, e.g., log the error and use a sensible default.
234+
Log.e(TAG, "Invalid numeric string encountered: ", e);
224235
}
225236

226-
routePoints.add(location);
227-
speeds.add(Double.parseDouble(nextLine[4]));
228-
if (maxSpeed == null || maxSpeed < Double.parseDouble(nextLine[4])){
229-
maxSpeed = Double.parseDouble(nextLine[4]);
230-
}
231237
}
232238
if (lineNumber > 1) {
233-
if (!nextLine[6].equals("null") && !nextLine[6].equals("")){
234-
engineTemps.add(Double.parseDouble(nextLine[6]));
235-
if (maxEngineTemp == null || maxEngineTemp < Double.parseDouble(nextLine[6])){
236-
maxEngineTemp = Double.parseDouble(nextLine[6]);
237-
}
238-
if (minEngineTemp == null || minEngineTemp > Double.parseDouble(nextLine[6])){
239-
minEngineTemp = Double.parseDouble(nextLine[6]);
239+
if (!nextLine[6].equals("null") && !nextLine[6].isEmpty()){
240+
try {
241+
engineTemps.add(Double.parseDouble(nextLine[6]));
242+
if (maxEngineTemp == null || maxEngineTemp < Double.parseDouble(nextLine[6])){
243+
maxEngineTemp = Double.parseDouble(nextLine[6]);
244+
}
245+
if (minEngineTemp == null || minEngineTemp > Double.parseDouble(nextLine[6])){
246+
minEngineTemp = Double.parseDouble(nextLine[6]);
247+
}
248+
} catch (NumberFormatException e) {
249+
// Handle the invalid input, e.g., log the error and use a sensible default.
250+
Log.e(TAG, "Invalid numeric string encountered: ", e);
240251
}
241252
}
242-
if (!nextLine[7].equals("null") && !nextLine[7].equals("")){
243-
ambientTemps.add(Double.parseDouble(nextLine[7]));
244-
if (maxAmbientTemp == null || maxAmbientTemp < Double.parseDouble(nextLine[7])){
245-
maxAmbientTemp = Double.parseDouble(nextLine[7]);
246-
}
247-
if (minAmbientTemp == null || minAmbientTemp > Double.parseDouble(nextLine[7])){
248-
minAmbientTemp = Double.parseDouble(nextLine[7]);
253+
if (!nextLine[7].equals("null") && !nextLine[7].isEmpty()){
254+
try {
255+
ambientTemps.add(Double.parseDouble(nextLine[7]));
256+
if (maxAmbientTemp == null || maxAmbientTemp < Double.parseDouble(nextLine[7])){
257+
maxAmbientTemp = Double.parseDouble(nextLine[7]);
258+
}
259+
if (minAmbientTemp == null || minAmbientTemp > Double.parseDouble(nextLine[7])){
260+
minAmbientTemp = Double.parseDouble(nextLine[7]);
261+
}
262+
} catch (NumberFormatException e) {
263+
// Handle the invalid input, e.g., log the error and use a sensible default.
264+
Log.e(TAG, "Invalid numeric string encountered: ", e);
249265
}
250266
}
251-
if (!nextLine[10].equals("null") && !nextLine[10].equals("")){
252-
if (endOdometer == null || endOdometer < Double.parseDouble(nextLine[10].replace(",", ""))){
253-
endOdometer = Double.parseDouble(nextLine[10].replace(",", ""));
254-
}
255-
if (startOdometer == null || startOdometer > Double.parseDouble(nextLine[10].replace(",", ""))){
256-
startOdometer = Double.parseDouble(nextLine[10].replace(",", ""));
267+
if (!nextLine[10].equals("null") && !nextLine[10].isEmpty()){
268+
try {
269+
if (endOdometer == null || endOdometer < Double.parseDouble(nextLine[10].replace(",", ""))){
270+
endOdometer = Double.parseDouble(nextLine[10].replace(",", ""));
271+
}
272+
if (startOdometer == null || startOdometer > Double.parseDouble(nextLine[10].replace(",", ""))){
273+
startOdometer = Double.parseDouble(nextLine[10].replace(",", ""));
274+
}
275+
} catch (NumberFormatException e) {
276+
// Handle the invalid input, e.g., log the error and use a sensible default.
277+
Log.e(TAG, "Invalid numeric string encountered: ", e);
257278
}
258279
}
259-
if (!nextLine[13].equals("null") && !nextLine[13].equals("")){
260-
if (endFrontBrakeCnt == null || endFrontBrakeCnt < Double.parseDouble(nextLine[13])){
261-
endFrontBrakeCnt = Integer.parseInt(nextLine[13]);
280+
if (!nextLine[13].equals("null") && !nextLine[13].isEmpty()){
281+
try {
282+
if (endFrontBrakeCnt == null || endFrontBrakeCnt < Double.parseDouble(nextLine[13])){
283+
endFrontBrakeCnt = Integer.parseInt(nextLine[13]);
284+
}
285+
} catch (NumberFormatException e) {
286+
// Handle the invalid input, e.g., log the error and use a sensible default.
287+
Log.e(TAG, "Invalid numeric string encountered: ", e);
262288
}
289+
263290
}
264-
if (!nextLine[14].equals("null") && !nextLine[14].equals("")){
265-
if (endRearBrakeCnt == null || endRearBrakeCnt < Double.parseDouble(nextLine[14])){
266-
endRearBrakeCnt = Integer.parseInt(nextLine[14]);
291+
if (!nextLine[14].equals("null") && !nextLine[14].isEmpty()){
292+
try {
293+
if (endRearBrakeCnt == null || endRearBrakeCnt < Double.parseDouble(nextLine[14])){
294+
endRearBrakeCnt = Integer.parseInt(nextLine[14]);
295+
}
296+
} catch (NumberFormatException e) {
297+
// Handle the invalid input, e.g., log the error and use a sensible default.
298+
Log.e(TAG, "Invalid numeric string encountered: ", e);
267299
}
268300
}
269-
if (!nextLine[15].equals("null") && !nextLine[15].equals("")){
270-
if (endShiftCnt == null || endShiftCnt < Double.parseDouble(nextLine[15])){
271-
endShiftCnt = Integer.parseInt(nextLine[15]);
301+
if (!nextLine[15].equals("null") && !nextLine[15].isEmpty()){
302+
try {
303+
if (endShiftCnt == null || endShiftCnt < Double.parseDouble(nextLine[15])){
304+
endShiftCnt = Integer.parseInt(nextLine[15]);
305+
}
306+
} catch (NumberFormatException e) {
307+
// Handle the invalid input, e.g., log the error and use a sensible default.
308+
Log.e(TAG, "Invalid numeric string encountered: ", e);
272309
}
273310
}
274-
if (!nextLine[32].equals("null") && !nextLine[32].equals("")){
275-
if (maxLean == null) {
276-
maxLean = Math.abs(Double.parseDouble(nextLine[32]));
277-
} else if (maxLean < Math.abs(Double.parseDouble(nextLine[32]))){
278-
maxLean = Math.abs(Double.parseDouble(nextLine[32]));
311+
if (!nextLine[32].equals("null") && !nextLine[32].isEmpty()){
312+
try {
313+
if (maxLean == null) {
314+
maxLean = Math.abs(Double.parseDouble(nextLine[32]));
315+
} else if (maxLean < Math.abs(Double.parseDouble(nextLine[32]))){
316+
maxLean = Math.abs(Double.parseDouble(nextLine[32]));
317+
}
318+
} catch (NumberFormatException e) {
319+
// Handle the invalid input, e.g., log the error and use a sensible default.
320+
Log.e(TAG, "Invalid numeric string encountered: ", e);
279321
}
280-
} else if (!nextLine[27].equals("null") && !nextLine[27].equals("")){
281-
if (maxLean == null) {
282-
maxLean = Math.abs(Double.parseDouble(nextLine[27]));
283-
} else if (maxLean < Math.abs(Double.parseDouble(nextLine[27]))){
284-
maxLean = Math.abs(Double.parseDouble(nextLine[27]));
322+
} else if (!nextLine[27].equals("null") && !nextLine[27].isEmpty()){
323+
if (!nextLine[15].equals("null") && !nextLine[15].isEmpty()){
324+
try {
325+
if (maxLean == null) {
326+
maxLean = Math.abs(Double.parseDouble(nextLine[27]));
327+
} else if (maxLean < Math.abs(Double.parseDouble(nextLine[27]))){
328+
maxLean = Math.abs(Double.parseDouble(nextLine[27]));
329+
}
330+
} catch (NumberFormatException e) {
331+
// Handle the invalid input, e.g., log the error and use a sensible default.
332+
Log.e(TAG, "Invalid numeric string encountered: ", e);
333+
}
285334
}
286335
}
287336
}
@@ -290,7 +339,7 @@ public void onSwipeRight() {
290339
lineNumber = lineNumber + 1;
291340
}
292341

293-
if (speeds.size() > 0){
342+
if (!speeds.isEmpty()){
294343
double avgSpeed = 0.0;
295344
for (double speed : speeds) {
296345
avgSpeed = avgSpeed + speed;
@@ -318,7 +367,7 @@ public void onSwipeRight() {
318367
tvBrakes.setText(frontBrakeText + "/" + rearBrakeText);
319368

320369
double avgEngineTemp = 0.0;
321-
if (engineTemps.size() > 0) {
370+
if (!engineTemps.isEmpty()) {
322371
for (double engineTemp : engineTemps) {
323372
avgEngineTemp = avgEngineTemp + engineTemp;
324373
}
@@ -331,7 +380,7 @@ public void onSwipeRight() {
331380
tvEngine.setText(Utils.toOneDecimalString(minEngineTemp) + "/" + Utils.toOneDecimalString(avgEngineTemp) + "/" + Utils.toOneDecimalString(maxEngineTemp) + " (" + temperatureUnit + ")");
332381

333382
double avgAmbientTemp = 0.0;
334-
if (ambientTemps.size() > 0) {
383+
if (!ambientTemps.isEmpty()) {
335384
for (double ambientTemp : ambientTemps) {
336385
avgAmbientTemp = avgAmbientTemp + ambientTemp;
337386
}
@@ -363,7 +412,7 @@ public void onSwipeRight() {
363412
Log.d(TAG,"Exception reading CSV: " + e.toString());
364413
}
365414

366-
if (routePoints.size() > 0) {
415+
if (!routePoints.isEmpty()) {
367416
MapsInitializer.initialize(getApplicationContext(), Renderer.LATEST, this);
368417
FragmentManager myFragmentManager = getSupportFragmentManager();
369418
SupportMapFragment mapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);

0 commit comments

Comments
 (0)