-3

I know how to read a text file, but I don't know how to read it orderly. For instance, how to read this" Tosca|Giacomo Puccini|1900|Rome|Puccini’s melodrama about a volatile diva, a sadistic police chief, and an idealistic artist|https://www.youtube.com/watch?v=rkMx0CLWeRQ"? Different information is separated by | , and I want them to display on different JLabel.

I did this

while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }   

What else should I do in the while loop?

Josiah Wu
  • 65
  • 11

1 Answers1

0

You need to split the string data using split function and put it to an array of string to get your desired data in group. Example: str = "10 | Patrick | 10-10-1996"

To store the data above and put it nicely into the database, you can split the string and store it to a class named 'Person' that contains data such as name,id, and birthday.

class Person{
    private int id;
    private String name;
    private String birthday;
    public Person(int id, String name, String birthday){
        //some setter code here
    }

    //some getter code here
} 

Then you can split each line of data you read in the text file.

while((line = bufferedReader.readLine()) != null) {
            String data[] = line.split("|")
            // data[0] to get the id you need
            // data[1] to get the name
            // data[2] to get the birthday
}
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • I tried what you suggest, but when I print data[0], there is only one letter shows up? I thought the string between | | should be printed. – Josiah Wu Nov 09 '16 at 17:31
  • Have you tried this string? String str = "10 | Patrick | 10-10-1996"? String data[] = str.split("|"); ( data[0] should return 10 when printed. ) – Kim Clarence Penaflor Nov 09 '16 at 18:07
  • The string between | will be printed if you have the "line" variable have the proper format. Try printing the "line" variable first if it contains '|' for splitting. – Kim Clarence Penaflor Nov 09 '16 at 18:14