I have multiple arrays and depending on the users input, which is stored in a string, I need to access the corresponding array. For example, the user types "/kit food" and presses enter, the string then gets sub-stringed to become "food". Lets say I already have an array called food and I want to print all of the items inside of it out into the console. here is an example code to what I'm trying to do:
String[] food = {"apple","carrot","pickle"};
String[] tools = {"hoe","shovel","rake"};
userInput = "food"; //already sub stringed from "/kit food"
for(String item : userInput){ //I need it to access the array food
System.out.println(item);
}
Would this be possible to do? or should I just manually code each if/else statement for every kit?
Using Java 7
public static void main(String[] args) {
String[] food = {"apple", "carrot", "pickle"};
String[] tools = {"hoe", "shovel", "rake"};
String userInput = "food"; //already sub stringed from "/kit food"
Map<String, String[]> map = new HashMap<>();
map.put("food", food);
map.put("tools", tools);
for (String item : map.get(userInput)) { //I need it to access the array food
System.out.println(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.