Waheed Rafiq
Waheed Rafiq

Reputation: 482

How to sub split when reading from text file

Rather a little bit confused

I have a text file that is formatted like this

FooBoo,male,20,10/04/1988

I know how to split a string for example

public loadData() {  

   String filepath = "G:\\Documents\\MEMove\\XXClients\\data.txt";

    BufferedReader bufReader = new BufferedReader(new FileReader(filepath));

    String line = bufReader.readLine();

    while (line != null) {

        // String[] parts = line.split("/");
        String[] parts = line.split(",");
        String part1 = parts[0];
        String part2 = parts[1];

        int part3 = Integer.parseInt(parts[3]);
        String [] sDOB = line.split("/");
        int sDOB1 = Integer.parseInt(sDOB[4]);
        People nPeople = new People(part1,part2,part3,sDOB1);

       readPeopleList.add(nPeople);
        line = bufReader.readLine(); 
    } //end of while 
    bufReader.close();

    for(People per: readPeopleList)
    {
        System.out.println("Reading.." + per.getFullName());

    }
}// end of method 

the problem is how do I split DOB / its not working for I am getting NumberFormatException Error

any ideas

thanks

Upvotes: 0

Views: 150

Answers (1)

Dorukhan Arslan
Dorukhan Arslan

Reputation: 2754

At first, split line using delimiter ",":

String line = "FooBoo,male,20,10/04/1988";
String[] parts = line.split(",");

Then, split last part using delimiter "\":

String dob = parts[parts.length - 1];
String[] sDob = dob.split("/");
for (String s : sDob) {
    System.out.println(s);
}

Edit: You can convert sDob into an ArrayList as follows:

ArrayList<String> sDobList = new ArrayList<>(Arrays.asList(sDob));

Upvotes: 1

Related Questions