Ad

While Loop Reading A File Running On An Extra Time

- 1 answer

Reading from a CSV file trying to get each line.

It will read all lines but run an extra time causing the `String' array to try and pull from a null Reference. the Data is 6 lines with 4 elements split with a ','.

public void setReadingArray(File localFile){
    Log.e("MyActivity","Reading into Array");

    InputStream is = null;
    try {
        is = new FileInputStream(localFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(DailyReadingActivity.this,"Could not find file Exception: "+ e, Toast.LENGTH_LONG).show();
    }
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8")));


    try {
        String line = reader.readLine();
        while (line != null) {

            Log.d("MyActivity", " Creating dailyReader ");
            //Split by ,

                String[] tokens;
                tokens = line.split(",");

                //Read Data
                DailyReading dailyReading = new DailyReading();
                dailyReading.setBookName(tokens[0]);
                dailyReading.setChapter(tokens[1]);
                dailyReading.setBookName2(tokens[2]);
                dailyReading.setChapter2(tokens[3]);
                listReadings.add(dailyReading);

                Log.d("MyActivity", " Just Created " + dailyReading);
                line = reader.readLine();


        }

    } catch (IOException e) {
        Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
        e.printStackTrace();
    }
    try {
        Log.d("MyActivity", " Close File 'is' ");
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 

 D/MyActivity:  Creating dailyReader Test,1,Test2,1
 D/MyActivity:  Creating dailyReader Test,2,Test2,2
 D/MyActivity:  Creating dailyReader Test,3,Test2,3-4
 D/MyActivity:  Creating dailyReader Test,1,Test2,1
 D/MyActivity:  Creating dailyReader Test,2,Test2,2
 D/MyActivity:  Creating dailyReader Test,3,Test2,3-4
 D/MyActivity:  Creating dailyReader 

New Code i have Changed to. Still having the same Issue.

    try {
        String line = "";
        DailyReading dailyReading = new DailyReading();
        while ((line = reader.readLine()) != null) {


            Log.d("MyActivity", " Creating dailyReader "+ line);
            //Split by ,
            String[] tokens;
            tokens = line.split(",");
                //Read Data
           // DailyReading dailyReading = new DailyReading();
            dailyReading.setBookName(tokens[0]);
            dailyReading.setChapter(tokens[1]);
            dailyReading.setBookName2(tokens[2]);
            dailyReading.setChapter2(tokens[3]);
            listReadings.add(dailyReading);

               // Log.d("MyActivity", " Just Created " + dailyReading);
            }


    } catch (IOException e) {
        Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
        e.printStackTrace();
    }

The Readout is:

 D/MyActivity:  Creating dailyReader Test,1,Test2,1
 D/MyActivity:  Creating dailyReader Test,2,Test2,2
 D/MyActivity:  Creating dailyReader Test,3,Test2,3-4
 D/MyActivity:  Creating dailyReader Test,1,Test2,1
 D/MyActivity:  Creating dailyReader Test,2,Test2,2
 D/MyActivity:  Creating dailyReader Test,3,Test2,3-4
 D/MyActivity:  Creating dailyReader 

as you can see it is running that extra time at the bottom that gives me the error, the While != null does not seem to work.

Ad

Answer

OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction

Ad
source: stackoverflow.com
Ad