Discerning what is a space in string..?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Hodan
    New Member
    • Feb 2011
    • 8

    Discerning what is a space in string..?

    So, I'm trying to make a method that takes an input stream (from a text file), and churns out each word into it's own string, and sticks it into a list. According to my logic this should work, but (obviously) it doesn't. Any help would be appreciated :)

    Code:
    private void countWords()
            {
                bool isSpace;
                count = 0;
                //Will run as long as the files is real.
                if (textFile != null)
                {
                    string line;
                    StreamReader lines = new StreamReader(textFile);
                    while ((line = lines.ReadLine()) != null)
                    {
                        //Makes sure the line isn't blank
                        if (line.Trim().Length != 0)
                        {
                            isSpace = false;
                            count++;
                            //Adds to the count and adds to the list
                            while(!isSpace)
                            {
                                if (string.Compare(line, blank) != 0)
                                {
                                    lines.Read();
                                    line = line + lines;
                                }
                                else
                                {
                                    isSpace = true;
                                }
                            }
                            randomOrder.Add(line);
                        }
                    }
                }
            }
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Can you define "This should work but it doesn't" please. Do you get an error? What does it do compared to what you need it to do.

    Comment

    • Allwin Jeba
      New Member
      • Jan 2011
      • 7

      #3
      assign a space to a constant = " " and use that constant to find in your string. eg.

      s.ToString().In dexOf(" ").
      if you get any index there.. it means there is space..

      Comment

      • Robert Hodan
        New Member
        • Feb 2011
        • 8

        #4
        Originally posted by Richard McCutchen
        Can you define "This should work but it doesn't" please. Do you get an error? What does it do compared to what you need it to do.
        What I'm trying to do is compare a string containing a space, to what I think (presuming I'm not using the streamreader wrong) should be the next character on a line. If the next character on the line is NOT a space, it adds it to the current string. When the string does hit a space, it adds the current word to a list, and continues, until it runs out of words.
        However;
        It freezes, gets trapped inside of this loop:

        Code:
        while(!isSpace)
             {
                  if (string.Compare(line, blank) != 0)
                  {
                       lines.Read();
                       line = line + lines;
                  }
                  else
                  {
                      isSpace = true;
                  }
        }

        Comment

        Working...