0

Edit SOLVED: I am being stupid. The input strings were longer than I thought (they were corrupted) in one case so some US data went into the European test and caused the exception when two doubles were concatenated together with a "." creating a string with three decimal points. Changed 7 to 8 and the original code is just fine.

Sorry to all who put time in on this issue.

Edited to add all code at end

I have a csv file that has european decimal values saved for example 12,345 I parse the data into a string and my code throws an exception. I have narrowed it down to one line

Here are the variables

String[] mark = line.split(",");
double wLat = 0;

This line won't work

wLat = Double.parseDouble(mark[3] + "." + mark[4]);

This line (for non eurpoean data) works

wLat = Double.parseDouble(mark[3]);

I am probably doing something stupid but this is difficult to Google and searching sa didn't help. I tried a string builder and append but parseDouble didn't work with a string builder. I would appreciate help on this.

Allen

Edit:

// Read old marks
    try {
        // read all the old mark settings so we end up with the last ones
        // this will restore settings on a restart
        BufferedReader br;
        br = new BufferedReader(new FileReader(baseDir + "/StartLine/Waypoint_log.csv"));
        String line = null;

        while ((line = br.readLine()) != null) {
            String[] mark = line.split(",");

            double wLat = 0;
            double wLong = 0;
            if (mark.length > 7){ // europe
                //wLat = Double.parseDouble(mark[3] + "." + mark[4]);
                //wLong = Double.parseDouble(mark[5] + "." + mark[6]);
            }
            else{ // normal case
                wLat = Double.parseDouble(mark[3]);
                wLong = Double.parseDouble(mark[4]);        
            }
            int index = Integer.valueOf(mark[0]);

            if (wLat >= -90 && wLat <= 90 && wLong >= -180 && wLong <= 180){
                StartLine2.waypointNameArray[index] = mark[1];
                StartLine2.waypointLat[index] = wLat;
                StartLine2.waypointLong[index] = wLong;
                StartLine2.waypointSet[index]=1;
                index++;
            }
        }
        br.close();


    }
    catch (Exception e) {

        Toast toast=Toast.makeText(this, "No Marks Set",Toast.LENGTH_SHORT);
        toast.show();
    }
Allen Edwards
  • 1,488
  • 1
  • 27
  • 44

3 Answers3

0

You're supposed to use OutputStream . Try using this sample:

OutputStream baos = new ByteArrayOutputStream();
baos.write(mark[3].getBytes(), 0, mark[3].getBytes().length);
mark[3]=".";
baos.writeOneByte(mark[3].getBytes()[0]);
baos.write(mark[4].getBytes(), 0, mark[4].getBytes().length);
wLat = Double.valueOf(baos.toString());

Hopefully, this would work

nstosic
  • 2,584
  • 1
  • 17
  • 21
0

try this...

    String s = "0,1,2,56,4567890";
    String[] strings = s.split(",");
    s = strings[3] + "." + strings[4];
    System.out.println(Double.valueOf(s));
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

I am being stupid. The input strings were longer than I thought in one case so some US data went into the European test and caused the exception when two doubles were concatenated together with a "." creating a string with three decimal points. Changed 7 to 8 and the original code is just fine. Sorry to all who put time in on this issue. By the way, it was a corrupt data file, 7 should have worked :-)

To be safe in the future, I changed the test to

if (!line.contains(".")){ // europe

If it fails this test, it is not a US number. The largest legal number is 180 so it would never get a "." if not a US type number.

Allen Edwards
  • 1,488
  • 1
  • 27
  • 44