Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Good day everyone, I don't know if there is an option after the return method to continue or if I should otherwise construct a code. I would like to after the return I continue to the end of the txt file and let the return list more values.

What I have tried:

public static string FindLineAboveAsterisks(TextReader reader)
        {
            StringBuilder sbBuilder = new StringBuilder();
            string result = reader.ReadLine();
            string line = String.Empty;
          

            while  (result is object && (line = reader.ReadLine()) is object)

        {
                int startIndex = 21;
                int length = 9;

            if (line.Contains("***"))

            {
                sbBuilder.AppendLine(result);
                return result;
                    
                }
                {
                    result = line.Substring(startIndex, length);
                }
                
            }
            return string.Empty;
Posted
Updated 5-Oct-20 23:29pm
Comments
Chris Copeland 6-Oct-20 4:35am    
Isn't this essentially the same question as you asked previously? Did you try using the answer given by Richard MacCutchan[^], as you marked it as solved?
dejf111 6-Oct-20 4:50am    
I've tried but I don't think I have the level
dejf111 6-Oct-20 4:53am    
I won't bother anymore, it's no use
dejf111 6-Oct-20 5:01am    
but thank you

No. The return statement terminates the method that it belongs to, so no more code can execute in there. As I told you yesterday at How do I write more than one entry?[^], you need to build a list of text lines inside this method. Start by creating a new List<string>. Then for each line that you need to save add it to the list. When you have read all lines of the file then return the List to the caller. That way you only need to process the input file once.
 
Share this answer
 
Comments
dejf111 6-Oct-20 5:02am    
I've tried but I don't think I have the level, but thank you
Richard MacCutchan 6-Oct-20 6:28am    
Well if you are going to give up that quickly your IT career is not likely to be very long. If you are not sure how to use some feature of the language then go and study the documentation*, search for articles etc. There is only one way to become proficient: study hard and practice, practice, practice.

*You are lucky today that OriginalGriff has provided a full implementation for you.
dejf111 6-Oct-20 6:52am    
I have bad that I'm not very proficient in the English language
Richard MacCutchan 6-Oct-20 7:24am    
That is not important - I expect I do not know (m)any words in your language. But most of the MSDN documentation is available in other languages so you should go there and check if your language is supported.
BillWoodruff 6-Oct-20 5:34am    
+5 Gold-star awarded for heroic patience :)
Try using an iterator method:
C#
public static IEnumerable<string> FindLineAboveAsterisks(TextReader reader)
{
    const int startIndex = 21;
    const int length = 9;

    string result = reader.ReadLine();
    string line;
    
    while (result != null && (line = reader.ReadLine()) != null)
    {
        if (line.Contains("***"))
        {
            yield return result;
        }
        
        // TODO: Validate that the line is long enough:
        result = line.Substring(startIndex, length);
    }
}

...

foreach (string line in FindLineAboveAsterisks(reader))
{
    Console.WriteLine(line);
}
 
Share this answer
 
Look at your code, please.
That looks like it was thrown together without any thought about what you are trying to do!

Throw it away, and think about your task: Read a file, find all the lines which are above lines with an asterisk, and return them.

So let's take it from the top: You need to return more than one line - so the obvious thing to do is to return a collection of strings instead of a single string. Because although you can return them as a single string, it makes life a lot harder for the code that calls your method - it has to "break it back up again" in order to use the information.
Let's change that:
C#
public static List<string> FindLineAboveAsterisks(TextReader reader)
Now, it returns a collection of strings so the outside world can work with it.
But ... why are you passing it a TextReader? That means each time you call it, the outside world has to do the work of creating, opening, passing, and closing the reader - which is silly. Pass the path instead, and let the method do what it wants with it:
C#
public static List<string> FindLineAboveAsterisks(string filePath)
Now, the caller looks easier to work with.
Let's start filling in the method: We need a List to return, and to process every line in a file. If we want to use every line, then let's just get them all and let the system handle it! That's pretty easy:
C#
public static List<string> FindLineAboveAsterisks(string filePath)
    {
    List<string> lines = new List<string>();
    foreach (string line in File.ReadLines(filePath))
        {
        // ...
        }
    return lines;
    }
Now, what do we have to do with the lines?
Simple; we need to collect all the lines where the next line contains three asterisks.
So we need to know what the last line was. Let's add that:
C#
public static List<string> FindLineAboveAsterisks(string filePath)
    {
    List<string> lines = new List<string>();
    string lastLine = "";
    foreach (string line in File.ReadLines(filePath))
        {
        // ...
        lastLine = line;
        }
    return lines;
    }

We need to check if the current line contains "***". If it does, add the last one to the collection. That's easy too - a quick if test will do it:
C#
public static List<string> FindLineAboveAsterisks(string filePath)
    {
    List<string> lines = new List<string>();
    string lastLine = "";
    foreach (string line in File.ReadLines(filePath))
        {
        if (line.Contains("***"))
            {
            lines.Add(lastLine);
            }
        lastLine = line;
        }
    return lines;
    }
Hang on ... it's finished, isn't it?

All we have to do now is call it and test it:
C#
string path = @"D:\Test Data\List of hats.txt";
foreach (string line in FindLineAboveAsterisks(path))
    {
    Console.WriteLine(line);
    }

Oh look - it works!

All I did was think about the task and break it down into "manageable chunks", each of which was pretty obvious, and easy to do.
And very quickly, we have working code.
So stop jumping right in, and think first: five minutes of planning can save you hours of working!
 
Share this answer
 
v3
Comments
dejf111 6-Oct-20 5:45am    
Thank you so much when it's done this gradually looks the simpler
OriginalGriff 6-Oct-20 6:07am    
That's what the basics of development are all about: thinking about the task and breaking it down into smaller, more manageable tasks - then deciding how they should interact, and what they need to "pass around" between them. That then decides what structures you will use, and how they are implemented. The actual coding is the quick bit! :laugh:
Richard MacCutchan 6-Oct-20 6:32am    
"The actual coding is the quick bit!"
That's easy for you to say. :)

BTW the above really is an excellent solution; almost an article.
OriginalGriff 6-Oct-20 6:38am    
:O
OriginalGriff 6-Oct-20 11:11am    
OK, so I tried:
https://www.codeproject.com/Articles/5282084/How-to-write-code-to-solve-a-problem-a-beginners-g
:laugh:
Quote:
I don't know if there is an option after the return method to continue or if I should otherwise construct a code.

The return is always the last instruction executed in given path.
C#
int Myfunc() {
// path 0
if (condition) {
  // path 1
  // do stuff
  return 1;
  // will never reach this point
  }
// path 0
// do stuff
return 0;
// will never reach this point
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900