Help Wanted Urgently!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helping hand
    New Member
    • Mar 2008
    • 1

    Help Wanted Urgently!!

    Hello! I need help with splitting the elements of a text file and storing each element in an array of its type. I have the following text file:
    The file follows the following format:

    Type Cmpy Type# ID msrp discount quantity


    The data in the file looks like this:

    TV Philips 101 133-32-434 5000 3000 5
    DVD Sony 612 414-55-908 8000 6000 13

    I need to split this text file and store all type elements in type[], all cmpy elements in cmpy[] and so on..

    SO my type array type[] will contain: TV, DVD,...
    my cmpy array cmpy[] will contain: Philips, Sony....
    Currently, I am able to read the data from the text file and display it as it is..but I am not able to split the file elements and store them in their own arrays..I tried everything from tokenizer to split() method and even scanner class..but have reached nowhere...pleas e help!!
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by helping hand
    Hello! I need help with splitting the elements of a text file and storing each element in an array of its type. I have the following text file:
    The file follows the following format:

    Type Cmpy Type# ID msrp discount quantity


    The data in the file looks like this:

    TV Philips 101 133-32-434 5000 3000 5
    DVD Sony 612 414-55-908 8000 6000 13

    I need to split this text file and store all type elements in type[], all cmpy elements in cmpy[] and so on..

    SO my type array type[] will contain: TV, DVD,...
    my cmpy array cmpy[] will contain: Philips, Sony....
    Currently, I am able to read the data from the text file and display it as it is..but I am not able to split the file elements and store them in their own arrays..I tried everything from tokenizer to split() method and even scanner class..but have reached nowhere...pleas e help!!
    First of all, the Scanner class sounds good. What exactly did you try?
    Reading a file works character after character, line after line. So, you'll have to add "TV" to type[], then add "Philips" to cmpy[] and so on, then add "DVD" to type[], "Sony" to cmpy[] etc. If you don't know, how many entries you'll going to have, an array is therefore not the best option. Try a list (e.g. ArrayList) or a Vector instead - they have variable size.
    To help you further, try again using the tips I gave you and then post your code and the problem(s) you're having, if it doesn't work. We'll be happy to help you.

    Greetings,
    Nepomuk

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by helping hand
      Type Cmpy Type# ID msrp discount quantity


      The data in the file looks like this:

      TV Philips 101 133-32-434 5000 3000 5
      DVD Sony 612 414-55-908 8000 6000 13

      I need to split this text file and store all type elements in type[], all cmpy elements in cmpy[] and so on..
      I strongly disadvise against such practices; a lot of people program Fortran in
      Java, i.e. they split up an object (or entity) in several parallel arrays where each
      array contains a single attribute of the original object.

      Build a little class instead that represents the ApplianceStock (or whatever) and
      store and manipulate those objects.

      You don't need to split that text file in a bunch of separate arrays. There should
      be a utility class that 'builds' an ApplianceStock object given such a descriptive
      line. That class (or object thereof) is a simple factory. The ApplianceStock object
      itself should know nothing of that text line.

      This world is full of crappy software that is full of arrays that are full of fragments
      of objects that actually belonged together in one object.

      kind regards,

      Jos

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        hello m8

        do this:
        let us say you will use the readline function
        when you read the line use a string tokenizer .
        in a while loop by using stringtokenizer read word by word
        you know the structure so store every word at its array

        GOOD LUCK

        hsn

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by hsn
          hello m8

          do this:
          let us say you will use the readline function
          when you read the line use a string tokenizer .
          in a while loop by using stringtokenizer read word by word
          you know the structure so store every word at its array

          GOOD LUCK

          hsn
          1.) Don't use StringTokenizer for splitting input. Read the docs for it to find out why.
          2.) Throw away those arrays and create proper objects first as has already been suggested above.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            Throw away those arrays and create proper objects first as has already been suggested above.
            I hereby solemnly swear that whenever I see another person ripping objects apart
            and wants to store their attributes in those Fortran arrays I'm going to perform
            dangerous creative things with my pink inflatable axe that says *beep!*.

            kind regards,

            Jos ;-)

            Comment

            Working...