C
C#3mo ago
GreenMile

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
No description
25 Replies
Angius
Angius3mo ago
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
GreenMile
GreenMile3mo ago
Ah, thats another error then
Angius
Angius3mo ago
You can provide a placeholder, though
string userInput = Console.ReadLine() ?? "";
string userInput = Console.ReadLine() ?? "";
In this case, if it were to be null, it will be an empty string instead
GreenMile
GreenMile3mo ago
Yes that solved the issue actually Mind explaining what it says in pseudocode or should I just google that Is it like a short circuit?
Angius
Angius3mo ago
Basically, something like
string userInput;
string? input = Console.ReadLine();
if (input == null)
{
userInput = "";
}
else
{
userInput = input;
}
string userInput;
string? input = Console.ReadLine();
if (input == null)
{
userInput = "";
}
else
{
userInput = input;
}
Richard
Richard3mo ago
If you want to limit to Y & N respectively, I would use this approach:
Angius
Angius3mo ago
It's a null-coalescing operator
Richard
Richard3mo ago
char userInput; do { Console.Write("Enter 'y' or 'n': "); userInput = Console.ReadKey().KeyChar; Console.WriteLine(); } while (userInput != 'y' && userInput != 'n');
Angius
Angius3mo ago
Let's chill with the loops for now, there's none in the code
GreenMile
GreenMile3mo ago
I appreciate your approach here, thanks so much for sharing these advices
Angius
Angius3mo ago
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
Richard
Richard3mo ago
true, but is a more direct solution to the problem.
Angius
Angius3mo ago
Visual Studio would've told you "hey, this is not a string actually, it's a string? so handle the null somehow"
GreenMile
GreenMile3mo ago
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
Richard
Richard3mo ago
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.
Angius
Angius3mo ago
Also, depending on the .NET version and project settings, VS might trat any reference type — like string — as nullable
GreenMile
GreenMile3mo ago
but 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
Angius
Angius3mo ago
In old versions string s = Console.ReadLine() will be fine. In newer versions, unless you disable it, it will show a warning
Richard
Richard3mo ago
@GreenMile Unhalted Exception? do you mean Unhandled?
GreenMile
GreenMile3mo ago
lol yes I do
Richard
Richard3mo ago
how are you aborting the loop?
GreenMile
GreenMile3mo ago
I honestly do not even know if that is what I am doing, lol but this is what shows up when trying CTRL Z
No description
Angius
Angius3mo ago
Yeah, Ctrl+Z makes Console.ReadLine() return null
Richard
Richard3mo ago
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.
jcotton42
jcotton423mo ago
To elaborate on this, Ctrl+Z in a console on Windows produces the end of file signifier.
Want results from more Discord servers?
Add your server