How to read from file and store some info into array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greyrainbow

    #1

    How to read from file and store some info into array?

    i'm really really new to Java. and i came across this question. and i don't know how to do. so if anyone would like to help me in the coding?

    Question: i was given a text file in this format.. and was asked to create CD arrays based on the text given. which in this case 4 CD arrays.


    [THE TEXT FILE (CD.TXT)]
    Code:
    4
    River Classic,Michael bubble,23.50,Crazy love,04:26,Hold on,04:01,Hollywood,03:10,At this moment,03:55
    Recovery Road,M&Ms,22.00,Love the way you lie,04:23,No love,05:00,Cinderalla Man,04:36
    Speak forever,Taylor Swifty,20.00,Mine,03:55,Speak now,04:03
    Best of Me,Eric Charles,18.50,River runs deep,03:33,Rocking chair,04:11,Autumn leaves,05:06,How deep is the ocean,04:02,Travelin' Alone,3:29
    [END OF TEXT FILE]

    the first number ('4') is the array of CD need to be created. then I need to spilt the info according to the CD title, singer, price, tracks [into arrays form] & tracks' length [into array form too]
    i did split the info [title, singer & price] but i don't know how to read & store the tracks & the tracks' length into the arrays.

    can someone help me?
    Last edited by Nepomuk; Nov 22 '10, 12:09 PM.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You can use FileInfupStream classes for this. And do you have a code for this?

    Regards
    Dheeraj Joshi

    Comment

    • greyrainbow

      #3
      i tried to do it like this.

      Code:
      public static void main(String[] argv) {
      
          BufferedReader br = new BufferedReader(new FileReader ("CD.txt"));
      
          int count = Integer.parseInt(br.readLine());
      
          String title = "";
          String singer = "";
          Double price = 0.0;
          String category = "";
          String discount = "";
          String tracks [] = new String [30];  // i just make 
          String length [] = new String [30];  // an array of 30s
      
           for (int i=0; i<count; i++) {
                  String s = br.readLine();
                  StringTokenizer st = new StringTokenizer(s, ",");
      
                  title = st.nextToken();
                  singer = st.nextToken();
                  price = Double.parseDouble(st.nextToken());
                  category = st.nextToken();
                  discount = st.nextToken();
      
                   System.out.println("Title: " + title + "\nSinger: " + singer + "\nprice: " + price +
                  "\ndiscount :" + discount + "\ncategory:" + category);
      after that, i dont know how to read the tracks & the lengths from the file & store them into the tracks[] and trackLength[] respectively.

      Comment

      Working...