-5

Hi all I have a CSV file like this:

 ban;frst_name;last_nam;adrs_provnc;accnt_type_id....
 ban1;frst_name1;last_nam1;adrs_provnc1;accnt_type_id....
 ....

I want to put this file into a datatype in which I can access each line as well as each field of everyline. Example: if I want the second line of the file then I should have --

"ban1;frst_name1;last_nam1;adrs_provnc1;accnt_type_id...."

And if I want the second field of second line I should have:

"frst_name1"

Is there a way I can do this , by which I can get access to both lines and fields.

This is what i coded

 public class cvsreader {    
List<List<String>> cust_data = new ArrayList<List<String>>(); // declared a list of list   
public void run() throws IOException {

    String cust_line = "";

    String delimeter = ";";

    String cust_inputfile = "C:/input.csv"; // input file


    BufferedReader cust_br = new BufferedReader(new FileReader(cust_inputfile));

    while ((cust_line = cust_br.readLine()) != null){

        cust_data = Arrays.asList(Arrays.asList(cust_line.split(delimeter))); // stored data from cust_line(string) to cust_data (list of list, so did type casting from list to array)
        System.out.println(cust_data);

        }

    }
}

it gives me output like below

[[ban,frst_name,last_nam,adrs_provnc,accnt_type_id....]]
[[ban1,frst_name1,last_nam1,adrs_provnc1,accnt_type_id....]]    
[[....                                                    ]]

now if i print

System.out.println(cust_data.get(0));

it gives output like this-

[ban,frst_name,last_nam,adrs_provnc,accnt_type_id....]    
[ban1,frst_name1,last_nam1,adrs_provnc1,accnt_type_id....]    
[....                                                    ]  

the one sqaure braces has been reduced but instead it should have given only the frst line.

[ban,frst_name,last_nam,adrs_provnc,accnt_type_id....]

and now if i print

System.out.println(cust_data.get(0).get(0));

it prints the below

ban    
ban1  

without square braces but instead it should have printed only the first field of first line that is-
ban.

please if anyone can now understand what actually has to be done to get desired result please suggest.

Anant
  • 3
  • 3
  • 3
    Let's see some code. –  Jul 13 '15 at 14:35
  • i dont have any code , i want to know how to do this – Anant Jul 13 '15 at 14:38
  • It seems like each row represents a User, or an Account, or something like that. So you just need a List. To access the first name of the third user, you would use `list.get(2).getFirstName()`. Google for "Java CSV parser" to find a library able to parse CSV, and use it to parse the file and transform each line to a User. – JB Nizet Jul 13 '15 at 14:39
  • 1
    search for `java parse csv` –  Jul 13 '15 at 14:39
  • 1
    @Anant, generally you get better results here by showing what you tried. It isn't always clear from a description what the constraints and requirements are, and SO isn't here to do your homework for you. Anyway, the answer with the links in it is your new starting point. –  Jul 13 '15 at 14:41

1 Answers1

0

This answer for reading line by line: How to read a large text file line by line using Java?

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

This answer for splitting a string: How to split a string in Java

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

This page for complete example of parsing CSV: http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

All code copy pasted from other SO threads.

Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
  • 1
    Don't just post URLs as the answer, include relevant information from the URLs in your answer in case the the URL changes or is no longer valid. – Shar1er80 Jul 13 '15 at 14:41
  • Sorry, was thinking since it was to SO pages the url would probably not change, and point to the relevant discussion instead of me taking it out of context. – JensB Jul 13 '15 at 14:45
  • thanks everyone, will search for CSV parser and let you know where i reach with that. – Anant Jul 14 '15 at 04:56
  • @JensB-The URL which you gave has an example of reading a a csv file spliting it by semicolan and printing its data. i want to split my file line by line and then field by filed. – Anant Jul 14 '15 at 05:01