Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement the method, which will read only letters and numbers, and which will write the characters to a StringBuilder.

So, for "01223123345,6789" the method should produce --> "01223123345"

public static StringBuilder ReadOnlyLettersAndNumbers(StreamReader streamReader)


I think I am close to the solution but I am forgetting something which causes 4 out of 7 tests to fail more specifically, for example, for this test case: "abc defg" The test expects 32 (I googled and found out that 32 corresponds to space character) but my method returns -1 (-1 is output when there are no characters to be read by streamReader.Peek() ).

Below i will attached official exercise requirements. I thought I did good job paraphrasing it but as it appears that's not the case.
Screen:
https://ibb.co/0K13JVr

Thank You in advance.

What I have tried:

public static StringBuilder ReadOnlyLettersAndNumbers(StreamReader streamReader)
      {
          StringBuilder result = new StringBuilder();
          char charLines;
          while (streamReader.Peek() > -1)
          {
              charLines = (char)streamReader.Read();
              if (char.IsLetterOrDigit(charLines))
              {
                  res.Append(charLines);
              }
          }

          return result;
      }
Posted
Updated 10-Mar-22 7:34am
v6
Comments
PIEBALDconsult 9-Mar-22 16:46pm    
Double-check your input.
I also wouldn't use .Peek, I'd use only .Read
PIEBALDconsult 9-Mar-22 17:59pm    
I also wonder whether or not the stream read everything into a buffer and then returned EOF when you Peek -- it shouldn't, but it might.
honey the codewitch 10-Mar-22 13:23pm    
I second PIEBALDconsult here. Where is your stream coming from? If it's coming from a network source Peek() will fail. You really shouldn't Peek() if you want your code to be robust. Use Read() and just keep a copy of the current character around in a variable instead of using peek.
Richard MacCutchan 11-Mar-22 4:02am    
See 1zx — ImgBB[^] for the proper requirements of the exercise.
honey the codewitch 11-Mar-22 4:45am    
I totally didn't even see the link when i first read the question. must have been having a day.

just replace
while (streamReader.Peek() > -1)

by
while (!streamReader.EndOfStream)
 
Share this answer
 
Comments
LuckyChloe 10-Mar-22 5:05am    
Thank you for the answer Mr. Luc
However, it still did not work. The same 3 tests pass and same 4 tests fail. Replacing
while (streamReader.Peek() > -1) with while (!streamReader.EndOfStream) did not change anything.
Screen:
https://ibb.co/fvfGYDQ
Luc Pattyn 10-Mar-22 6:13am    
Four points:
1) you don't need Peek(); it exists so you could create a look-ahead parser, which isn't what you are needing here. Therefore the EndOfStream property is the right way to detect the end of the stream.
2) you rightfully are using char.IsLetterOrDigit, so why would space equals 32 be any relevant?
3) you seem to misunderstand the job at hand; the failing test cases stop scanning the input as soon as a non-letter-or-digit is encountered, something your code does not do at all.
4) You did not provide us with the correct requirements; giving a succeeding case is good, giving a failing case without the expected output isn't.
LuckyChloe 10-Mar-22 6:53am    
I am sorry. I tried my best to provide full requirements. Will update my question now. Thank You
Your code still continues if it sees a character that is neither a letter nor a digit. It should be:
C#
while (!streamReader.EndOfStream) // read until end of stream
{
    charLines = (char)streamReader.Read(); // get the next character
    if (char.IsLetterOrDigit(charLines))   // if it is a letter
    {
        res.Append(charLines); // add it to the new string
    }
    else
    {
        break; // the character is neither a letter nor a digit so stop reading
    }
}
 
Share this answer
 
Comments
LuckyChloe 10-Mar-22 7:47am    
Thank you Mr. Richard for the answer. But it still does not work. Still only the same 3 tests pass as before. I get different error message though.
Screen:
https://ibb.co/5Kzh2Wv
Richard MacCutchan 10-Mar-22 8:02am    
It would appear that the test system is expecting a return value of the character that stopped the scan, or -1 if the scan read to the end of stream. But you have not provided the full details of the test requirements, so that is just a guess.
LuckyChloe 10-Mar-22 8:34am    
I see. Will update my question again then. This requirement was not written in the exercise instruction so that's why I missed it.
Richard MacCutchan 10-Mar-22 9:09am    
"The method should return the value of the character that stopped the scan, or -1 if the scan is read to the end of stream."
That is not what the TestCase statements imply. For example, The failing test has the following control statement:
[TestCase("abc defg", 32, ExpectedResult = "abc")]

But the error message states:
Expected 32 but was 100

Which suggests it is expecting both values.
LuckyChloe 10-Mar-22 9:12am    
100 corresponds to char "d"
32 corresponds to char " "

I think it is expecting 32 and not 100
Thank you for finally providing specs that do make sense.

I don't need a StringBuilder, there is no point in accumulating the good characters.
I don't use Peek() as explained before.

C#
public static int GetValueOfFirstNonLetterNonDigit(StreamReader streamReader) {
    while (!streamReader.EndOfStream) {
        int val = streamReader.Read();
        if (!char.IsLetterOrDigit((char)val)) return val;
    }
    return -1;
}



You still need to fix your test cases, as the expected values don't satisfy the requirements at all.


Further optimization for you to study:

C#
public static int GetValueOfFirstNonLetterNonDigit(StreamReader streamReader) {
    while (true) {
        int val = streamReader.Read();
        if (!char.IsLetterOrDigit((char)val)) return val;
    }
}
 
Share this answer
 
v4
Comments
Richard MacCutchan 10-Mar-22 12:34pm    
Luc, I am not sure it is that simple. Take a look at 3z — ImgBB[^].
Luc Pattyn 10-Mar-22 12:43pm    
Thanks for pointing that out.
The OP written specs are wrong again then.
It is getting simpler by the minute, we should only watch for a given value.
So return of the StringBuilder and goodbye IsLetterOrDigit()
Luc Pattyn 10-Mar-22 12:46pm    
And now I'm wondering how the first three test cases ever passed, as even the number of method parameters isn't matching ???

Figured it out, the second image defines the job, the first one attempts to solve it.
LuckyChloe 10-Mar-22 13:03pm    
Hey Luc thank you for the answer but I am not allowed to modify tests - Nunit tests.

public static StringBuilder ReadOnlyLettersAndNumbers(StreamReader streamReader)

This part of the code must not change or I won't be able to submit the exercise to the program which grades it. Also, I am certain that the code for tests and test cases itself are correct since somebody from the course managed to make all 7 tests green without changing Tests.cs file
LuckyChloe 10-Mar-22 13:06pm    
Below i will attached official exercise requirements. I thought I did good job paraphrasing it but as it appears that's not the case.
Screen:
https://ibb.co/0K13JVr

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