1

I keep getting an ArrayIndexOutOfBoundsException, but I am not sure what part of code exactly causes this error. Any help will be appreciated a lot. The code is

        while ((line = reader.readLine()) != null) {

            String[] wordOnLine = line.split(",");
            geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3])));

        }

The error is

Unable to start activity ComponentInfo{swindroid.suntime/swindroid.suntime.ui.Main}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

Alfabravo
  • 7,232
  • 6
  • 43
  • 77
nadia
  • 11
  • 1
  • You are spliting line by -','. Then you are reffering to wordOnLine- are you sure every line ends with coma? Also check this one: http://stackoverflow.com/questions/454908/split-java-string-by-new-line – X3Btel Oct 25 '16 at 15:27
  • Thanks, for your reply. Yes, every lines needs to have camma as I have to read the following text from a file. Glenmore Park,-33.79068,150.6693,Australia/Sydney – nadia Oct 25 '16 at 15:32
  • @nadia I think you need have 2 looping, 1. for split per line by `\n` and 2. looping until `line.length` for split your text per line by `,` – Sodiq Oct 25 '16 at 17:24

2 Answers2

0

public List getListOfLocations() {

//  String fileName = "au_locations.txt";
//  BufferedReader reader = null;
//  StringBuilder builder = new StringBuilder();

    InputStream input = getResources().openRawResource(R.raw.au_locations);
    BufferedReader reader = new BufferedReader( new InputStreamReader((input)));
    String line;
    StringBuilder builder = new StringBuilder();
    File file = new File(getFilesDir(), "au_locations.txt");

    try {
    //  reader = new BufferedReader(new InputStreamReader(getAssets().open(fileName)));
    //  String line;

        while ((line = reader.readLine()) != null) {
        //  builder.append(line).append("\n");
            String[] wordOnLine = line.split(",");
            geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3])));
            //locations.add(line);
        }

        if(file.exists()){
            FileInputStream inputFile = openFileInput("au_locations.txt");
            input = inputFile;
            reader = new BufferedReader((new InputStreamReader((input))));
            while ((line = reader.readLine()) != null) {
                //      builder.append(line).append("\n");
                String[] wordOnLine = line.split(",");
                geo.put(wordOnLine[0] , new GeoLocation(wordOnLine[0], Double.parseDouble(wordOnLine[1]), Double.parseDouble(wordOnLine[2]),TimeZone.getTimeZone(wordOnLine[3])));

                //locations.add(line);
            }
        }
    //  return locations;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
nadia
  • 11
  • 1
  • 1
    Please add extra info to the question, not as an answer – Alfabravo Oct 25 '16 at 15:26
  • This is what I am trying to do. Create an app to allow the user to add a custom location. That is, they should be able to provide a Name, Latitude/Longitude and a Timezone. The time zone should be obtained as a offset from GMT. The location provided must be persisted to a file, and read back when the application is reloaded again – nadia Oct 25 '16 at 15:45
0

public void AddLocationHandler() throws IOException {

    String name = ((EditText) findViewById(R.id.nameText)).getText().toString();
    String longi = ((EditText) findViewById(R.id.longText)).getText().toString();
    String lati = ((EditText) findViewById(R.id.latText)).getText().toString();
    String timezone = ((EditText) findViewById(R.id.timeText)).getText().toString();

    Double locationLong;
    Double loctaionLat;
    TimeZone locationTimeZone;

    try {
        loctaionLat = parseDouble(lati);
    }catch (NumberFormatException e)
    {
        Toast toast = Toast.makeText(getApplicationContext(), "not a valid latitude ", Toast.LENGTH_SHORT);
        toast.show();
        return;


    }
    try {
        locationLong = parseDouble(longi);
    }catch (NumberFormatException e)
    {
        Toast toast = Toast.makeText(getApplicationContext(), "not a valid longitude ", Toast.LENGTH_SHORT);
        toast.show();
        return;


    }
    if(timezone.matches("(\\+|\\-)[0-1][0-9]\\:[034][05]")){
        locationTimeZone = TimeZone.getTimeZone("GMT" + timezone);
    }
    else
    {
        Toast toast = Toast.makeText(getApplicationContext(), "not a valid Time Zone ", Toast.LENGTH_SHORT);
        toast.show();
        return;
    }
    File file = new File("au_location.txt");

    if(!file.exists()){
        file = new File(getFilesDir(), "au_location.txt");
    }
    FileOutputStream fos = openFileOutput("au_locations.txt", Context.MODE_APPEND);
    OutputStreamWriter f = new OutputStreamWriter(fos);

    f.write(name + "," +loctaionLat + "," + locationLong + "," + "GMT" + timezone + "\n");
    //fos.write(s.getBytes());
    //fos.close();
    Toast.makeText(getBaseContext(),"Data Saved", Toast.LENGTH_LONG).show();

    f.flush();
    f.close();

    Toast.makeText(getBaseContext(),"New Location Added", Toast.LENGTH_LONG).show();



}
nadia
  • 11
  • 1