Simple Java Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newjavastudent
    New Member
    • Oct 2008
    • 1

    Simple Java Question

    [color=#0040BF]Dear All,

    I have a huge text file with name animal.txt, I have the following sample data:

    >id1
    lion
    >id2
    horse
    cat
    >id3
    mouse
    tiger


    I need to save the contents of this file in an array or String without the id line. i.e
    string1 = "lion"
    string2= "horse
    cat"
    string3= "mouse
    tiger"

    So how can I store the data in string or array without the id line .. just only the names?

    Thanks for your cooperation

    [/color]
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi newjavastudent! Welcome to bytes.com!

    It's great to have you here!

    When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.

    About your question: You can search for certain things in a String, e.g. the indexOf(String str) method mentioned in the API should do the job in your case. Read the description in the Link I gave you and you should be able to solve the rest of the problem yourself.

    Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

    Greetings,
    Nepomuk

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      First, you need to load the file and store it as a string in memory.
      Then you can use String methods to find and replace characters.
      Read about "regular expressions", they are very useful here.
      Then you need to store the string back on disk as a file, overwriting the old one.

      Try to write your own load and store methods and regular expressions.
      It's not that complicated or long (my code is has 7 lines for reading a string, and 1 line for text change by using a regular expression), and you will learn a lot.
      If you are stuck, list them here what you have done so far, and I will correct them or will give you more hints or will show you the code that solves your problem.

      Tip:
      for reading a file, you should make a re-usabel method:
      "public static String readFileAsStrin g(String sourceFileName) throws IOException"
      This method contains source code that uses following classes:
      - class File, methods: File(filename), exists()
      - class FileInputStream , methods: FileInputStream (name), available(), read (byte[] data)
      - class String, methods: new(byte[] data)
      - class IOException, methods: IOException(err orMessage)

      Happy programming,
      Chris

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by chaarmann
        First, you need to load the file and store it as a string in memory.
        [...]
        Then you need to store the string back on disk as a file, overwriting the old one.

        Try to write your own load and store methods and regular expressions.
        It's not that complicated or long (my code is has 7 lines for reading a string, and 1 line for text change by using a regular expression), and you will learn a lot.
        Another Tip: We actually have a howto on how to read and write files in Java here. Try to follow that tutorial and you should learn a lot and get your task solved at the same time! :-)

        Greetings,
        Nepomuk

        Comment

        Working...