-3

Say I have an array of string items, {"item1", "item2", "item3"} and some Boolean values named the same:

Boolean item1 = true
Boolean item2 = true
Boolean item3 = false

I need to go through the array and stop at a designated spot and be able to return the Boolean value of the item corresponding to the string i am at.

How would you go about doing this?

degant
  • 4,861
  • 1
  • 17
  • 29
Richard
  • 1
  • 1
  • 2
    You should use a hashmap for this, i cant figure out a way to check if a string value is equal to a variable name. – Tacolibre May 29 '17 at 10:38
  • 2
    you could use reflection... But that´s far from how one should implement something like that and the whole base idea seems to not be that good to begin with – SomeJavaGuy May 29 '17 at 10:39
  • 1
    Your question seems to be related already to a part of a planned solution. What is the original problem you try to solve? Maybe there is another way. – Henry May 29 '17 at 10:42

4 Answers4

2

Are item1, etc. local variables or fields? In the first case there is no way: Java doesn't do dynamic variable names. Use e.g. Map<String, Boolean> instead. In the second case you can use Class.getDeclaredField method, but it's quite likely there's a better way to achieve your purposes.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
1

The easy method:

public class BoolArray {

    public static void main(String[] args) {
        boolean result = checkItem("item1");
        System.out.println(result);
    }
    //or without using return type print false in sysout
    public static boolean checkItem(String name){

        String items[] = {"item1", "item2", "item3"};
        boolean hasItem[] = {true, true, false};

        if(name.equals("item1")){
            return hasItem[0];
        }else if(name.equals("item2")){
            return hasItem[1];
        }else if(name.equals("item3")){
            return hasItem[2];
        }else{
            return false;
        }
    }

}

Or you can use advanced methods(like collection API) then you can reduce code lines. You can use HashTable HashMap, from these you can work with key, value pairs. Or you can use List with your own custom Pair.

Using HashTable:

You can implement this as a method and get specific value as above one.

Hashtable table = new Hashtable();
table.put("item1", true);
table.put("item2", true);
table.put("item3", false);

Enumeration e = table.keys();
while (e.hasMoreElements()) {
  String key = (String) e.nextElement();
  System.out.println(key + " : " + table.get(key));
}
Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • What is array contains 50+ entries, are we supposed to write 50 else-if combinations? – Mehraj Malik May 29 '17 at 11:16
  • He didnot mention that but he can use for-each loop. If you need to see I can update the answer – Blasanka May 29 '17 at 11:23
  • I just show him a way he can combine with other answer and achieve his task. Also I mention in my answer that he can use advance method saying this: "then you can reduce code lines" Is it fine @MehrajMalik – Blasanka May 29 '17 at 11:25
0

You can create a function which returns boolean :

boolean[] b_arr = {true,true,false};
String[]  s_arr = {"I1","I2","I3"};

public boolean check(){
for(int i=0;i<s_arr.length;i++){
    if(logic_to_check_your_condition){
    return b_arr[i];
    }
 }
return false;
}
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
0

While technically possible, it might be better to take a different approach. For example:

Map<String, Boolean> map = new HashMap<>();
map.put("item1", true);
map.put("item2", true);
map.put("item3", false);

String[] array = new String[] {"item1","item2","item3"};

for(String item:array){
    //do something with the string

    boolean youNeedToStop = "item2".equals(item); //you mentioned that you want to stop at some point
    if(youNeedToStop){
        System.out.println("Item: " + item);
        System.out.println("Attached Boolean: " + map.get(item));
        break;
    }
}

Here we make a map from String to Boolean. Then, whenever you need the boolean that belongs to a given String, you can get it from the map.

Thijs Steel
  • 1,190
  • 7
  • 16