How do you get a value for a key in java?

Hello everyone,
Can anyone tell me how to get a value for a key in java ?
I need it in my extension.
Any help is appreciated !
Thanks.

2 Likes

Want to get values from appinventor from dictionaries to extension? do you just need to save something in the extension as a key-value?

You can use HashMap:

HashMap<String, Integer> saveData = new HashMap<String, Integer>();

Use:

saveData.put("apple",2);
saveData.remove("apple");
saveData.containsKey("apple");
saveData.get("apple");
5 Likes

Any imports needed ?

2 Likes

java.util.HashMap

1 Like

Thanks, trying it now itself and I'll tell you the result.

2 Likes

Your method is working. Thank you !

2 Likes

Oops, its showing 'cannot find symbol' - 'get'. How to solve it ?

2 Likes

What and where does it show? PrintScren and show code.

[javac] /compiler/androidX/src/kppin/com/aquib/searchy/Searchy.java:76: error: cannot find symbol
[javac] res = text.get("answer");
[javac] ^
[javac] symbol: method get(String)
[javac] location: variable text of type String
[javac] 1 error

2 Likes

It's not a problem with "get", just java basics ...

String res = text.get("answer");

Tried that too......
Not working.

2 Likes

Make such code and compile. "Not working" is not enough.
If the value is of type Integer, it must be:
int res = text.get("...");

1 Like

[javac] /compiler/androidX/src/CsZdF/com/aquib/searchy/Searchy.java:75: error: cannot find symbol
[javac] String res = text.get("answer");
[javac] ^
[javac] symbol: method get(String)
[javac] location: variable text of type String
[javac] 1 error

2 Likes

Show me the HashMap initialization line.

HashMap<text, text> saveData = new HashMap<text, text>();
:thinking:

2 Likes

Is it consistent with what I wrote in post 2?

HashMap<String, String> text = new HashMap<String, String>();

Now the key and value must be of type String.

And now:

text.put("apple", "two");
text.remove("apple");
text.containsKey("apple");
String value = text.get("apple");

Its string only.

2 Likes

You are given as on a tray, copy and paste - must work.

Just a min.... trying...

2 Likes

Its showing cannot find symbol - 'HashMap'

[javac] /compiler/androidX/src/KyBYH/com/aquib/searchy/Searchy.java:75: error: cannot find symbol [javac] HashMap text = new HashMap(); [javac] ^ [javac] symbol: class text [javac] /compiler/androidX/src/KyBYH/com/aquib/searchy/Searchy.java:75: error: cannot find symbol [javac] HashMap text = new HashMap(); [javac] ^ [javac] symbol: class text [javac] /compiler/androidX/src/KyBYH/com/aquib/searchy/Searchy.java:75: error: cannot find symbol [javac] HashMap text = new HashMap(); [javac] ^ [javac] symbol: class text [javac] /compiler/androidX/src/KyBYH/com/aquib/searchy/Searchy.java:75: error: cannot find symbol [javac] HashMap text = new HashMap(); [javac] ^ [javac] symbol: class text [javac] 4 errors
2 Likes