• Post Reply Bookmark Topic Watch Topic
  • New Topic

String value to be the name of Variables created  RSS feed

 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have the value of an entered String to be set as the name of the String to be created whilst running.

For example: String username=Keyboard.readString() & the user entered for example the word: "Hello".Is there a way how I can make the Java Program create another String named Hello (Inputted value of String username).

If this is allowed, what do I have to use and if possible show me exactly what I have to do with the example mentioned above?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

No it's not allowed.
Why do you want a String variable called "Hello" or whatever is entered for the username and what value would this variable hold?
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch.

No it's not allowed.
Why do you want a String variable called "Hello" or whatever is entered for the username and what value would this variable hold?


I was building this small program for school where I have to do a 'simple' registration (username and pasword) and login system.

My idea was that I would have a string enteredusername=password so in the log in it checked the string username with the password directly instead of searching one by one. Is there another method I can do to create a registration and login system.

Note: I don't want this to be a one time registration but stores usernames and passwords until I stop the active tool.

My source code so far:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you use a Map. Check out the Java collection classes ie http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Yes you use a Map. Check out the Java collection classes ie http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html


Thanks for your support!

But I've already been there and couldn't really figure out which section I needed to try to figure out and work out. If you find some time can you show me with this example: that the value of string temp should be the name of a string.

Don't call me lazy. But I haven't even been studying Java for 2 months and I sometimes not able to understand everything but some examples and try to analyse it and figure it out usually helps me. Thanks!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like:


You repeat the second line for each username and password pair you want to put into the map and then when you want to check a password for a username do some thing like:

 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:something like:


You repeat the second line for each username and password pair you want to put into the map and then when you want to check a password for a username do some thing like:



Actually my idea was something more like this:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I'm not sure what that code is supposed to be doing in reference to your original question. Can you explain please.

BTW:
1. You don't need to assign "" to the variable to 'clean' it. Whatever value is assigned through Keyboard.readString() with overwrite any existing value anyway.
2. If (temppass=username) { ... } this is an assignment and not a test of equality and even if you had used == it's still wrong as Strings should be tested for equality using their equals() method and not ==.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want to do cannot be done in Java. Besides, you're complicating things too much when it doesn't need to be.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Sorry but I'm not sure what that code is supposed to be doing in reference to your original question. Can you explain please.

BTW:
1. You don't need to assign "" to the variable to 'clean' it. Whatever value is assigned through Keyboard.readString() with overwrite any existing value anyway.
2. If (temppass=username) { ... } this is an assignment and not a test of equality and even if you had used == it's still wrong as Strings should be tested for equality using their equals() method and not ==.


Yes my mistake in both points you mentioned in 1 & 2. But is still the same referred as in the original question. The value which I will be having in temp, I would like to create a String named the same as for example:

If John was entered by the user in the String temp...

temp="John". //I want another string to be created named John
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:What you want to do cannot be done in Java. Besides, you're complicating things too much when it doesn't need to be.


I've heard both sides: can't be done and be done with maps. So I'm kinda confused now. Can you tell me another method how I can accomplish this please?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have not quite understood the responses. The only way you could change the names of identifiers in the code is to write and compile a .java file on the fly.
You can simply put names against values in a Map:-You can read about Maps in the Java® Tutorials. Strings are a popular choice for the left member of the pair to put (called the key) because Strings are immutable; if you use a mutable key and it changes its state you might never find it back. You can use any valid type for the right element, called the value.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:
Junilu Lacar wrote:What you want to do cannot be done in Java. Besides, you're complicating things too much when it doesn't need to be.


I've heard both sides: can't be done and be done with maps. So I'm kinda confused now. Can you tell me another method how I can accomplish this please?

You can't do what you want. That is, you can't dynamically generate a new variable named after a value entered by a user.
However you can store key-value pairs in a Map which effectively gives you the same end result. That is, you can use a Map to simulate what you want to do.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you may have not quite understood the responses. The only way you could change the names of identifiers in the code is to write and compile a .java file on the fly.
You can simply put names against values in a Map:-You can read about Maps in the Java® Tutorials. Strings are a popular choice for the left member of the pair to put (called the key) because Strings are immutable; if you use a mutable key and it changes its state you might never find it back. You can use any valid type for the right element, called the value.


Tony Docherty wrote:What you want to do cannot be done in Java. Besides, you're complicating things too much when it doesn't need to be.


So maps are basically a 'link' between two related information such as Username & Password. That's exactly what I need then! The only problem I'm having is applying them to the code already mentioned in an above post and where I should place it to work. I've already tried placing

just after case 1 but was giving me an error about Map class & HashMap class.

Can someone let me know how I should place them and configure them to the code I have above?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error was it giving you? Have you added import statements to import the classes?
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:What error was it giving you? Have you added import statements to import the classes?


I will check about it tomorrow and try to work it out and set it up. No, can you please provide with the import statements requird to import the classes please.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:No, can you please provide with the import statements requird to import the classes please.

Would you like to try to find yourself first? There are some advantages when you looking for an information.
So likely you would find some other useful stuff closely related with an information exactly you're looking for, so you'd learn something new.
Also, you'd have a chance to discover some new information sources for the future questions. Plus it is a good habit.

Some useful sources for you (1st about the import statements, 2nd java api, where you could find an information about packages, classes, methods you're interested in):
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
http://docs.oracle.com/javase/7/docs/api/
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:I will check about it tomorrow and try to work it out and set it up. No, can you please provide with the import statements requird to import the classes please.

As Liutauras Vilda has quite rightly said, try to work it out for yourself before asking for help. We will help as much as we can but we won't spoon feed you. Learning requires an amount of effort on your your part and whilst I appreciate how frustrating these sort of problems are when you start out unless you attempt to fix them yourself you'll never learn. If I give you the import statements then you won't have learn't how to work out what import statements to include and next time you use a new class you will be asking for the import statements again. Read http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html and see if you can work out what to include.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Brandon Ciappara wrote:No, can you please provide with the import statements requird to import the classes please.

Would you like to try to find yourself first? There are some advantages when you looking for an information.
So likely you would find some other useful stuff closely related with an information exactly you're looking for, so you'd learn something new.
Also, you'd have a chance to discover some new information sources for the future questions. Plus it is a good habit.

Some useful sources for you (1st about the import statements, 2nd java api, where you could find an information about packages, classes, methods you're interested in):
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
http://docs.oracle.com/javase/7/docs/api/


Tony Docherty wrote:What error was it giving you? Have you added import statements to import the classes?


Campbell Ritchie wrote:I think you may have not quite understood the responses. The only way you could change the names of identifiers in the code is to write and compile a .java file on the fly.
You can simply put names against values in a Map:-You can read about Maps in the Java® Tutorials. Strings are a popular choice for the left member of the pair to put (called the key) because Strings are immutable; if you use a mutable key and it changes its state you might never find it back. You can use any valid type for the right element, called the value.


So I was able to complete to the following:



Though I'm having the following error:
C:\Users\Brandon\Documents\JavaPrograms\LoginRegister.java:37: error: variable myMap might not have been initialized
String pwd = myMap.get(username);

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating an instance of HashMap in case 1 so myMap will be null if case 2 is executed first.

BTW Please change the name myMap to something more meaningful ie to describe the data the map holds.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:You are creating an instance of HashMap in case 1 so myMap will be null if case 2 is executed first.

BTW Please change the name myMap to something more meaningful ie to describe the data the map holds.


Ok will change myMap to something more meaningful. So what can I do to solve the problem. What I tried doing is to initialize it before but then it said it was already initialized
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:What I tried doing is to initialize it before but then it said it was already initialized

Please show us the code where you are initializing the Map. And what exactly do you mean by "it said it was already initialized"? Did you get a compile-time error? Did you get a runtime error? What was the exact error message (copy-paste from the console output)?
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Brandon Ciappara wrote:What I tried doing is to initialize it before but then it said it was already initialized

Please show us the code where you are initializing the Map. And what exactly do you mean by "it said it was already initialized"? Did you get a compile-time error? Did you get a runtime error? What was the exact error message (copy-paste from the console output)?




C:\Users\Brandon\Documents\JavaPrograms\LoginRegister.java:37: error: variable myMap might not have been initialized
String pwd = myMap.get(username);


The error is display as I click run, but the program still 'launches' working like prior the error. I don't think I initialized good. Imagine that I didn't initialize it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create your HashMap once and only once and so it needs to be done before you ask the user to input values. Currently every time option 1 is pressed you are overwriting myMap with a new HashMap and so any values in the original map will be lost.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. My small project is now complete:

 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:My small project is now complete

From the one point of view, from the other - still there are places where you can improve it.

1. Import statement. Looking at the each line of your code you should be able to answer, what is it doing and why? You can answer that is importing everything what is in a package java.utils, but why? It is difficult to answer while you're not familiar with all the code. So, it is better to omit wildcard import statements, as it makes ambiguities, which classes you're using, it makes your code less readable and more difficult to debug if necessary.
2. Code indentation not good enough. Makes your code even less readable.
3. Code is not commented. After couple of weeks you won't be able even for yourself to explain what is written in your code. Code readability issues again.
4. "menu=Keyboard.readInt();" not really clear what Keyboard is, and where it comes from. So the program even doesn't compile. Maybe it is not a full source code, not sure about that.

Summary: the code still can be improved a lot. And it is important to do that, otherwise you can take bad habits of writing not readable code, which is wrong in general.

Probably well experienced guys could give you even more advices, which can be priceless for the way you think about programming.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much more serious problem: such a long main method.
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Much more serious problem: such a long main method.


Objects and method is the next topic that we should start doing at school so I try to fix it once I learn how to properly do it. Thanks!
 
Brandon Ciappara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Brandon Ciappara wrote:My small project is now complete

From the one point of view, from the other - still there are places where you can improve it.

1. Import statement. Looking at the each line of your code you should be able to answer, what is it doing and why? You can answer that is importing everything what is in a package java.utils, but why? It is difficult to answer while you're not familiar with all the code. So, it is better to omit wildcard import statements, as it makes ambiguities, which classes you're using, it makes your code less readable and more difficult to debug if necessary.
2. Code indentation not good enough. Makes your code even less readable.
3. Code is not commented. After couple of weeks you won't be able even for yourself to explain what is written in your code. Code readability issues again.
4. "menu=Keyboard.readInt();" not really clear what Keyboard is, and where it comes from. So the program even doesn't compile. Maybe it is not a full source code, not sure about that.

Summary: the code still can be improved a lot. And it is important to do that, otherwise you can take bad habits of writing not readable code, which is wrong in general.

Probably well experienced guys could give you even more advices, which can be priceless for the way you think about programming.


About the important statements: At school we rarely use imports and when I searched some forums to see how I can get map working mostly agreed to do what I did. Also, what do you mean by 'wildcards'? About the code indentation I'm planning to watch a video on YouTube to see how it should be done. And yes you're definitely right about the in-line comments, will make sure to be added and a good practice for next projects. I have a keyboard.class: hate the scanner so I use it to faster my work.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My take on comments:

1. Write as few as possible. Instead, concentrate on making your code SO CLEAR that you don't need to write comments because the code is self-documenting. This is why choosing the right names is very important. Comments almost always become obsolete and most developers ignore them anyway.

2. Comments can be useful in explaining WHY you did something. Comments that explain WHAT the code is doing should be made redundant by self-documenting code.

Compare this:

Versus this:

IMO, it's much easier to follow the refactored code.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Ciappara wrote:Also, what do you mean by 'wildcards'?

I meant import java.util.*

So you could change that by "import java.util.HashMap"and "java.util.Map", so it would be clear straight away, what classes you're going to use in your code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wildcards is a term used for "a character that represents one or more other characters". have you ever done something like: dir fred* to get a list of all files that start with "fred"? the '*' character here is a wildcard that matches anything. There are other wildcards you can use, but that is the most common. You did this: While not wrong, and while it does do what you want, it isn't the clearest it could be, because someone like me doesn't know which parts of the java.util package you are using. You can see here that there are a LOT of things you could be using. By explicitly breaking out the two you want (per Liutauras's suggestion), it lets others know exactly what you will be using.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also potential problemsThat code won't compile because there is a Timer class in each of the above packages.

If you ever need to use two classes with the same simple name, you have to use their fully qualified names.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the topic is closed, I hope I will not confuse OP with my question. It is meant for Junilu and
has nothing to do with OP's topic.

Junilu Lacar wrote:My take on comments:

1. Write as few as possible. Instead, concentrate on making your code SO CLEAR that you don't need to write comments because the code is self-documenting. This is why choosing the right names is very important. Comments almost always become obsolete and most developers ignore them anyway.

2. Comments can be useful in explaining WHY you did something. Comments that explain WHAT the code is doing should be made redundant by self-documenting code.

Compare this:
(....)
IMO, it's much easier to follow the refactored code.


hi Junilu,

I was wondering: do you think your second code is more clear than the first code,
because of the removal of the comments? My impression is that it is because you
refactor your code. I do not see why the removal of the comments is relevant for that.
Could you elaborate a little? Thanks!

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
My impression is that it is because you refactor your code.

Exactly. Many agile developers consider comments as a code smell indicating that code could be made clearer and more expressive by refactoring. If you refactor the code so that it's clear and self-documenting, then you don't need the comments because they don't add anything to what the code is already saying.

My point was that I prefer to put more effort in making the code clean and expressive rather than writing comments to help the reader understand what's going on. I find that code that has lots of comments can be even more difficult to read because not only do I have to try to decipher what the code does, my brain and eyes have to work harder to ignore the comments. Then I have to decide whether or not the comments match my understanding of the code. When they don't match my understanding, I have to go back and check if I have really misunderstood the code or if the comments are just obsolete or poorly written. I always distrust code comments, especially ones that I didn't write.

If I do write comments, it's usually to explain why I decided to do something in a certain way. I'll also put up notices like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!