Getting value from an string(special way)
Hello, thank you for taking time to look at my thread.
I want to make a program that look like this:
publicclass test{
int aa=2;
int bb=5;
int cc=1;
String toread="cc";
publicstaticvoid main(String args[]){
aa = toread;//this is not what i want, i want that aa shall be the same
//as cc but i want the aa to read from toread first to see wich int
//aa should become equal to (in this case cc).
}
}
Please reply if you know how i would do ( that i described in the comments).
Thank you.
If I understand you, you are trying to identify a variable by storing the variable name in a string. That's convoluted to say the least.
Better: tell us what you are trying to do. What your goal is. What, not how.
(1) Use reflection
(2) Wrap the ints in an Integer object and put them in Hash with a name as their Key.
>If I understand you, you are trying to identify a variable by storing the >variable name in a string.
Yes thats what im trying to do, sorry for not being correct.
Can you please tell me how to do now?
> (1) Use reflection
> (2) Wrap the ints in an Integer object and put them
> in Hash with a name as their Key.
if you are using 1.5 or later you don't have to wrap the ints, just use them as is.
tsitha at 2007-7-29 19:20:41 >

>> (1) Use reflection
>> (2) Wrap the ints in an Integer object and put them
>> in Hash with a name as their Key.
>if you are using 1.5 or later you don't have to wrap the ints, just use >them as is.
What do you mean : just use them as is?
Can you please write an simple example code?
Thank you
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
tsitha at 2007-7-29 19:20:41 >

Im actually really opposed to Autoboxing (im not kidding) so I dont use it.
import java.util.*;
public class NamedNumbers{
public static void main(String[] args){
new NamedNumbers();
}
public NamedNumbers(){
map = new HashMap();
set("aa", 1);
set("bb", 2);
set("cc", 3);
display();
set("bb", "cc");
display();
}
public void display(){
System.out.println("aa: " + get("aa"));
System.out.println("bb: " + get("bb"));
System.out.println("cc: " + get("cc"));
}
public int get(String key){
return ((Integer)map.get(key)).intValue();
}
public void set(String key, int value){
map.remove(key);
map.put(key, new Integer(value));
}
public void set(String key1, String key2){
map.remove(key1);
map.put(key1, new Integer(get(key2)));
}
HashMap map;
}
> Im actually really opposed to Autoboxing (im not
> kidding) so I dont use it.
>
Why are you opposed to it?
tsitha at 2007-7-29 19:20:41 >

The removes arent really necessary and:
map.put(key1, new Integer(get(key2)));
can probably just be
map.put(key1, map.get(key2));
since i think Integers are immutable(?) so you can
use an obj reference instead of a new one.
and theres error handling left.
So, JavaGuy, don't use autoboxing. Problem solved?
what does autoboxing have to do with anything, lol.
how did that even come up?
> what does autoboxing have to do with anything, lol.
> how did that even come up?
When you said "(2) Wrap the ints in an Integer object and put them in Hash with a name as their Key. "
and I said "if you're using 1.5 or later you don't have to wrap them"
and then you said you didn't like it, and I asked you why... remember now?
tsitha at 2007-7-29 19:20:41 >

>So, JavaGuy, don't use autoboxing. Problem solved?
Nah, this HashMap is new to me and im so confused of all your solutions. So all cancel the project for now. When i have more experience in java (HashMap) ill go back to this link.
>> remember now?
i see. i see.
but what does my response to your response to my post have
to do with anything?
The phrase "this thread has gone pear-shaped" comes to mind.
> Nah, this HashMap is new to me and im so confused
whaaaaaaaat? suit yourself but it wont get any easier than that.
whats confusing about: int x = get("aa") ?
i have an almost religious belief that all things should be in hashmaps.
hashmaps should be in hashmaps. hashmaps should even be
keys that return themselves. i usually just subclass the classloader
and load everything into a hashmap.
too much soda for me...
>whaaaaaaaat? suit yourself but it wont get any easier than that.
>whats confusing about: int x = get("aa") ?
>i have an almost religious belief that all things should be in >hashmaps.
>hashmaps should be in hashmaps. hashmaps should even be
>keys that return themselves. i usually just subclass the classloader
>and load everything into a hashmap.
>too much soda for me..
Hey hey let me cancel it if i want to , do you think i can just use your code without knowing anything about how it all worked, as i said im new to HashMaps
Yesterday I replied to a post with:
public class MapOMatic extends HashMap < String, MapOMatic > {...
Mappy enough fo ya?
>> Hey hey let me cancel it if i want to
you know, when baby birds display your callous quititude their
moms push them off the nest and if they dont fly they fall to their
deaths.
so yea, its just a thought, you might want to flap a wing or two.