#duplicate name String&ArrayList

74 messages · Page 1 of 1 (latest)

wide solar
#

First of all i am new here so if I document anything wrong, please forgive me.
Problem:
I want to create a program. This Program should create a List which gets the name of the of the value of the "userInput" String which is the input of the user.
For Example -> user gets asked for input and gives Input like, "listOne" so the List should get the name "listOne"

Thanks

glad vaultBOT
#

This post has been reserved for your question.

Hey @wide solar! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fiery eagle
wide solar
fiery eagle
#

Give the list another name

#

And the name of the list doesn't matter

#

Well it should be descriptive of what the list contains

#

But you shouldn't name a variable after user input

wide solar
#

ok i get your point. But I want to create a program which can create ArrayLists as soon a the user wants it. So that this process is automated and in this case is there any other approach, because the list that gets created automatically needs to get a name

#

should have described my whole plan right away

#

@fiery eagle

fiery eagle
#

All you need to do is put user input and access it at a later time

remote raptor
#

you cannot use a String as the name of a variable

#

variable names can only be specified directly

marble totem
#

(unless you're up for using bytecode manipulation, but you generally never need to rename a variable)

wide solar
#

Ok, and how do I define the variable name when it should get created automatically?

#

is there any way to do this?

ivory scroll
#

you choose one

#

the value gets created dynamically. the variable does not

#

your program creates the list when the user wants to, not the variable

wide solar
#

sorry if I ask a lot, I dont know how long you normally discuss on a problem.
I really want to understand it 100%

ivory scroll
#

the name is static, the variable is not

#

a variable declaration says, when the variable is created, the variable should have name x
a local variable gets created dynamically, whenever the declaration is reached

wide solar
#

and if the user decides to create another list?

#

a second one

wide solar
#

i dont get the second part

ivory scroll
#

there is no second part

ivory scroll
# wide solar and if the user decides to create another list?

it gets put in the "same" variable. local variables are discarded when they go out of scope, ie where they can be accessed from
a variable defined as a function parameter can only be accessed inside that function, a variable defined inside a loop can only be used inside that loop

#

if you don't store it somewhere else, the first arraylist is just lost along with the variable

#

think of function parameters like i mentioned, they're created dynamically each time you call the function, with the same name each time

wide solar
#

ok that helped me a lot with my understanding of scopes

#

is there a way to create an ArrayList with a function for example. which gets a name assigned and not hard coded?

ivory scroll
#

no, and it's not useful to do so at all

wide solar
#

ah alright

ivory scroll
#

also you don't assign names to the list

#

you assign names to where the list goes

#

you can achieve dynamic "naming" with a map, but that's not particularly useful in this case either

wide solar
#

good to know, i am kind of new to java

ivory scroll
#

actually might be depending on what you actually need to do, you weren't very clear on that

#

but dynamic naming on variables isn't useful.

wide solar
#

should i explain my whole idea of the program?

#

briefly

ivory scroll
#

don't think that'll be necessary

#

do you need to persistently store the arraylist along with the given name

#

for other uses in your program later

wide solar
#

I want to create a program which can be used by anyone.
They should be able to create a List, for example a shopping list.
And be able to create another List, for example workout plan

#

like a small "to do" program

cosmic bough
#

and those lists should be saved and read back into the program later, right?

wide solar
#

later i wanted to implement a GUI

ivory scroll
#

....i asked a yes/no question, because that's all the info i need

wide solar
ivory scroll
#

you'll want a map for that, not variables.

ivory scroll
wide solar
ivory scroll
cosmic bough
wide solar
#

my idea would have been to create a function which can be called and then creates a list

#

problem would have been, when the function gets called a second time

#

because the name would be duplicated

#

is my understanding correct, that this would not work?

#

because you know the name is hard coded

wide solar
#

my problem was just that i had the idea that i just described

ivory scroll
#

variable names just do not matter to the runtime

#

they generally don't matter to the user, either

#

they only really matter to the developer

cosmic bough
#

they only exist in the compiled program to make debugging easier