0

I am getting a null value when im reading from my teachers. csv file. Column 1 which is att[0] works but att[1] returns 3 null values.

My csv looks like this: 1, Mr Murphy 2, Mr Davis 3, Ms Simpson Each on separate lines ie line 1 -> 1, Mr Murphy etc

Here is my code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadCSV
{
    public static void main(String[] args)
    {
        //Input file which needs to be parsed
        String readTeachers = "teacher.csv";
        BufferedReader fileReader = null;

        //Delimiter used in CSV file
        final String DELIMITER = ",";

        try
        {
            String line = "";
            //String line = inFile.readLine();
            //Create the file reader
            fileReader = new BufferedReader(new FileReader(readTeachers));
            int count=0;
            String[] att = new String[10];
            //Read the file line by line
            while ((line = fileReader.readLine()) != null) 
            {
                //Get all tokens available in line
                String[] tokens = line.split(DELIMITER);

                int i=0;

                count++;
                for(String token : tokens)
                {
                    att[i] = token;
                    i++;
                    //Print all tokens
                   // System.out.println(token);

                    System.out.println(att[1]);
                    break;
                }


            }
            //System.out.println(count);
            //System.out.println(att[1]);
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally
        {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3
  • You're printing out att[1] even on the first iteration, when you've only assigned a value to att[0]... so yes, at that point it will be null. Perhaps you wanted to print out att[i]? Have you executed this step by step in the debugger, and looked at tokens for each line? Commented Feb 13, 2016 at 16:22
  • I printed att[1] out of the for loop and still gives me a null value, but i am able to print att[0] out of the for loop Commented Feb 13, 2016 at 16:26
  • So use a debugger and look at what's in tokens... Commented Feb 13, 2016 at 16:37

1 Answer 1

1

Your issue here is that you have a break statement inside the for loop that exits the loop at the end of the first iteration. Therefore, you are only putting a value in the first index of your array. Take out that statement and it should be fine.

            for(String token : tokens)
            {
                att[i] = token;
                i++;
                //Print all tokens
               // System.out.println(token);

                System.out.println(att[1]);
                break; // <---- ***take this out***
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.