Get a line from a textfile and use some values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djpaul
    New Member
    • Oct 2006
    • 137

    Get a line from a textfile and use some values

    Hello,
    I'm creating a program for school.
    But it's works with a tekst file.
    In a certain section i need some values of a tekst line to draw lines.
    Te line is someting like:
    345543; Old wood; $12,50 ; 1; (200, 400, 700, 1200); (300, 700, 200, 1350)

    So now i need the numbers between the () to draw lines in a picturebox.
    How can i get those values into an array? or integer....?

    Thanks!
    Paul
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    If using C#

    This solution is far from perfect but should be ok for school:

    read the line into a string variable, then get the index of the fist bracket:
    Code:
    int StartIndex = myString.IndexOf("(");
    now get the index of the "," seperating the numbers:
    Code:
    int Length = myString.IndexOf(",", StartIndex) - StartIndex ;
    now make a substring of the number:
    Code:
    string MyCoOrdString = myString.Substring(StartIndex+1, Length);
    Cast the String to an Integer for drawing:
    Code:
    int CoOrdinate = Convert.ToInt32(MyCoOrdString);
    also consider using an integer array to store the co-ordinates. use a loop based on the index of the ")" to break from the function which retrieves the numbers.

    repeat this process for each number, like I said it is far from perfect but fairly easy to understand and implement

    Comment

    • djpaul
      New Member
      • Oct 2006
      • 137

      #3
      Okay thanks.
      i will try it.
      Think i have to use a do while loop for it and an extra counter..

      Gr Paul

      Comment

      • djpaul
        New Member
        • Oct 2006
        • 137

        #4
        Okay, but now i have a problem
        I can look trought the string and add the values but sometimes there are more values between the ()
        Like this:

        ......... (400,600,700,12 00)
        Or.
        ..........(500, 399,600,300);(5 90,365,347,727) ;(379,476,1500, 1750)

        With a maximum of 10 that can me stored.

        Who has some suggestions?

        Thanks!

        Regards,
        Paul

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          the Split method of strings will be really useful here. Basically, you need to set up an array of the chars/strings that you want to split your strings around. In this case, first you'd want to split around ';' and then you'd want to split around parenthesis and commas.

          Here's a basic example:
          Code:
          string toSplit = "string.to;split";
          char[] delims = { ';', '.' };
          string[] tokens = toSplit.Split(delims);

          Now tokens has 3 strings: "string", "to" and "split". Note that you can split around single or multiple delimiters. Also, if you split around a char that doesn't exist, you result in only one string. You don't necessarily need to check for the existence of the delimiters before splitting by them.

          What I'd do if I were you, is split around the semicolon first. Now you have an array of each set. Make a System.Collecti ons.ArrayList. Then, loop through all the resulting strings, even if there is only one, and split each result around parenthesis and commas. Now you have an array that holds just the numbers in a single set. Add the resulting array to your ArrayList. Now you can loop through your ArrayList to handle each of your point sets.

          Hope that helps.

          Comment

          Working...