Beginner questions. prevent unhalted exception
I am really in the most beginner stage you can imagine though I have been messing around with other languages before so I know some stuff. I want to effectively use a beginner friendly option to prevent any other input but "y" or "n" to be accepted, which works fine, however null **values, CTRL + Z will still cause error.** Any advice without introducing classes, preferable a control flow based solution to prevent this null; (CTRL + Z ABORT) from breaking it? Here is my code: https://onecompiler.com/csharp/42qqt9jk5
25 Replies
Well, it throws an error on
.Trim().ToLower()
Because it's possible you're trying to call it on null
Console.ReadLine()
returns a string?
not a string
Ah, thats another error then
You can provide a placeholder, though
In this case, if it were to be
null
, it will be an empty string insteadYes that solved the issue actually
Mind explaining what it says in pseudocode or should I just google that
Is it like a short circuit?
Basically, something like
If you want to limit to Y & N respectively, I would use this approach:
It's a null-coalescing operator
char userInput;
do
{
Console.Write("Enter 'y' or 'n': ");
userInput = Console.ReadKey().KeyChar;
Console.WriteLine();
} while (userInput != 'y' && userInput != 'n');
Let's chill with the loops for now, there's none in the code
I appreciate your approach here, thanks so much for sharing these advices
One of the reasons why it's a good idea to use a nice and proper IDE when learning, instead of some random online compiler, btw
true, but is a more direct solution to the problem.
Visual Studio would've told you "hey, this is not a
string
actually, it's a string?
so handle the null
somehow"I use VS actually I just wanted to share my code online for u, but yea, I tried mess around with string ? and while loops to fix it but still couldn't wrap my head around the issue
The bonus of the do ... while approach is that it prevents any other characters from being accepted, so you don't have all the edge cases to worry about.
Also, depending on the .NET version and project settings, VS might trat any reference type — like
string
— as nullablebut will it prevent unhalted exception because I tried with just accepting y or n with a while loop, yet abort it would still break it
In old versions
string s = Console.ReadLine()
will be fine. In newer versions, unless you disable it, it will show a warning@GreenMile Unhalted Exception?
do you mean Unhandled?
lol yes I do
how are you aborting the loop?
I honestly do not even know if that is what I am doing, lol but this is what shows up when trying CTRL Z
Yeah,
Ctrl+Z
makes Console.ReadLine()
return null
ok. So what's happening is this... When you press CTRL Z, you are allowing null item to be passed to your variable. Then when you try to output your variable, it is null = has no value, and hence the compiler doesn't know what to do and throws an exception.
With the loop I shared, the loop will never exit unless one of the values is y or n.
so there's no way to get a null.
If it does throw an exception, change the while portion to : while (userInput == null || (userInput != "y" && userInput != "n")
there you will first check if it's a null value, then continue loop.
If it's not null, it will check if it's not y or n, then continue.
Also ReadLine() should be used if entering a line of text, versus ReadKey() is more suited to single key presses.
To elaborate on this, Ctrl+Z in a console on Windows produces the end of file signifier.