Valid Inputs
can someone help me understand what i’m doing wrong? I can’t figure out how to get the user input to work..
47 Replies
what's not working and what would "working" look like?
working would look like this
and what's the current problem?
the problem is that i can’t figure out how to make the user input values
like i don’t know what is wrong in my coding that prevents the the function getValue to operate
Passing 10 to your list constructor doesn't do what you think it does
yeah i was gonna get to that eventually, still trying to work out what the current bug is
are you getting an error message?
(pssst, use
.Add()
on the list)it's better to guide them through diagnosing the issues instead of just telling them how to fix stuff :when:
do i leave the constructor blank?
Yes.
even if the total amount of inputs you want is 10?
But read up on what both of those alternatives do
lists work differently than arrays in terms of how they're sized and how you put new values in
my instructor teaches more by like examples and following along. i struggle when i don’t understand concepts
Your code looks like what I would expect for an array, not for a list.
do i use foreach?
No.
If you want to use a list, read up on how lists work
(i still don't know what "prevents the function getValue to operate" means in terms of what the code is actually doing wrong)
like getValue is supposed to prompt the user to enter “inputs” to determine whether they’re considered valid or invalid but i guess there’s something wrong with my main method
@Jimmacle
Well, the loop won't run
The list has no elements in it
The count is 0
so the loop isn’t even relevant to the list? should i comment it out?
loops are relevant when you want to do something multiple times
so your next step is to look at your loop conditions to see why it's running 0 times, which goes back to pob's comment about looking into how lists should be used
right and i want the user input to loop.
Fix that
we're getting to that
so @Dread , how do we add an item to a list?
would i put inputValues.Add
Thats a good start
so, if I wanted to add... 5 things?
List<String> inputValues = new List<String>()
inputValues.Add()
inputValues.Add()
inputValues.Add()
inputValues.Add()
inputValues.Add()
?
List<String> inputValues = new List<String>(5?)
inputValues.Add()
inputValues.Add()
inputValues.Add()
inputValues.Add()
inputValues.Add()
And how would you do that without repeating the same line 5 times?
loop?
Yes
what does that 5 in the parentheses mean? because there’s 7 things?
It initializes the list with the capacity of 5
See, a list, underneath, is an array of a constant length
Whenever that length gets exceeded when you
.Add()
to that list, a new array of double that length gets created and all the elements are copied over
To avoid having to do that, if you know beforehand how big your list will be, you can initialize the list to a certain capacity
That will set the length of the internal array
Creating a new List<T>(5)
creates a list backed by a 5-long array
When adding a 6th element to it, the list resizes the array to one of length 10
And so onU should change something in the for loop as zzzzzz mentioned the list is empty so for loop is not executing
Think about what conditions should the for loop be
this is where i’m at and i’m still unsure that ive even made much progress. is it my main method that needs fixing? or is it my getValue loop?
there is a red squiggle under the most immediate issue
how many times do you want the loop to run?
Where is 'inputvalues' defined?
Have a think, the for loop adds number to the list, so how many time you want to add to the list, and what would the condition of the for loop be?
Your original code is almost fine, with a slightly change to the for loop
@daniel2 how do i loop inputValues.Add?
originally your code is:
but currently
inputValues.Count
is 0
because no element is inside the list
so the for loop is not executing
so you need to change the condition of the for loop
instead of inputValues.Count
think how many time you need to loop@daniel2 10 loops because i want 10 inputs right?
yeahhhhh