❔ ✅ Issues With Type Conversion When Validating User Input
In this section of code, I print items to the screen and request user input. I'm trying to basically go
if user input is not an integer, decrement tries and recall function. If user input is an integer but that integer does not match an index of List<string> initialSelection, then decrement tries and recall function. Otherwise, if user input is an integer and is a correct index of the list, then start next function
but I'm having issues with the validation and type conversion. Thanks20 Replies
I'm having issues with the validation and type conversion.what issues?
it's not working? I don't really know how to explain it other than what I'm trying to do
If user input is an integer but that integer does not match an index of List<string> initialSelection, then decrement tries and recall function. Otherwise, if user input is an integer and is a correct index of the list, then start next function
what's not working?
which part of that?
checking whether the user input is an integer?
checking whether it's an index of a
List<string>
?
decrementing and retrying?
starting the next function?this works. However, when the user actually puts in an integer, and checking if it's an index of the list like
it tells me that I
Cannot implicitly convert type 'string' to 'bool'
but I'm not trying to do that. I'm trying to check if it's an index of the list.
like my else if part is to check if the integer entered is an index of the list. Since the index is 0-2 and the user enters a number 1-3, I subtract one from the users input to check if it's an index, but it's not letting me
just like if I write it like this
it tells me the same thing. So I'm having issues converting the users input to an integer. I have also tries to wrap Convert.ToInt32()
around user selection and it won't let me do that either
so my question is, how do I check if the users input is an integer, and if not -> show error, if so -> check if its an index and if not -> show error while decrementing the same tries variable
checking whether the user input is an integer?- that part works
checking whether it's an index of a List<string>?- can't get conversion right to take user input as an integer to then check the index
decrementing and retrying?- it's not decrementing the same tries variable. Instead it'll decrement one version for when it's not an integer and then reset when it is an integer. It needs to be the same variable
starting the next function?- no I got that part down like it keeps reading
int.TryParse(Console.ReadLine(), out userSelection)
as a string instead of an integer so I'm trying to convert it back to an integer so that I can use it to check if the index is correct.
I just want to get the users input, check if it's an integer and if so then check if that integer is an index of the list. If it's not an integer then decrement tries and recall function. If it is an integer but not an index, then decrement the same tries count and recall the function.
^alright
so
yes, you are getting a type error here
Cannot implicitly convert type 'string' to 'bool'
that's becase you are passing a string value to a statement that expects a boolok so then if I Have
how do I go inside of my if statement and check if it's a correct index of the list?
what counts as a correct index of the list?
if the user inputs a number 1, 2, or 3 then it would subtract 1 from the users input to check if the input matches index 0, 1, or 2. If it doesn't, that means they're out of list index range and the error message needs to show, decrement tries, and recall the function <- or allow the user to try again in other words
so
what are the valid index values that you can accept?
..... 1, 2, or 3
great
check for those
yep great idea
better yet
check in a way that accounts for you chaning the list in the future
I.E. don't assume the list has 3 items
so I did this, but when I put in 4 as a response, it background the console window and told me the list index was out of range inside of the IDE instead of displaying the error message
check in a way that accounts for you chaning the list in the futureI don't know what this means but if I put in 3 for my user input, it works just fine I changed my nested if statement to say
if (userSelection >= 0 && userSelection <=4)
and it seems to work ok
thank you for your help ❤️that would allow for values 0, 1, 2, 3, and 4
which is not 1, 2, and 3
ergo, exception
because the menu option shows 1, 2, 3, 4 but the list indexes 0, 1, 2, 3
the return subtracts 1 from the users input to get the correct index
okay
so, why is 0 accepted?
i fixed it to say 1
okay
so... then it's fixed
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.