i built this function that takes a line of code and puts it into an arraylist and returns that list. problem is when I try to input the string into the list when I debugged the code it showed me that what was being inserted was- [[Ljava.lang.String;@1786f9d5] -how can I fix this? my code is:
public static List<String[]> GetAll(String st){
String[] list = st.split("bags contain | contain |bags, | bag, | bags\\.| bag\\.");//ways to split it-shouldn't be changed
List<String[]> newlist = new ArrayList<>();
for (int i = 1; i < list.length; ++i) {
if (!list[i].equals("no other")) {
int n = Integer.parseInt(list[i].split(" | \\s")[0]);
for (int j = 0; j < n; ++j) {
newlist.add(list[i].split(" \\s[0-9],\\s "));//here is the problem I think when I try to remove the number from the string
}
}
}
if(newlist.size()>1) {
return newlist;
}
return new ArrayList<>();
}
public static void main(String[] args) {
HashMap<String,List<String[]>> hm = new HashMap<>();// can kinda ignore the hashmap
String input=reader.nextLine();
while(!input.equals("-1")){
hm.put(GetName(input),GetAll(input));
input=reader.nextLine();
}
}
public static String GetName(String st){
return st.split("bags")[0].stripTrailing();
}
static boolean isInArr(String[] arr,String looking){
for (String s:arr){
if(s.strip().equals(looking.strip())){
return true;
}
}
return false;
}
some examples, each line in a new string, the first few words should be ignored as well:
sidenote. here "shade/name color"(at the beginning of the string before "contain") is not relevant so the function overall should skip over it- meaning in the arraylist which is returned "shade/name color" will not appear
here "dull indigo" is not relevant so the function overall should skip over it st= "dull indigo bags contain 2 faded black bags, 2 faded red bags, 4 dim bronze bags."
--> function should return
[faded black, faded black, faded red, faded red, dim bronze, dim bronze, dim bronze, dim bronze]
here "pale blue" is not relevant so the function overall should skip over it st= "pale blue bags contain 2 pale fuchsia bags, 5 mirrored bronze bags, 2 faded fuchsia bags."
-- function should return
[pale fuchsia, pale fuchsia, mirrored bronze, mirrored bronze, mirrored bronze, mirrored bronze, mirrored bronze, faded fuchsia, faded fuchsia]
here "light lime" is not relevant so the function overall should skip over it st= "light lime bags contain 4 pale lime bags, 5 plaid green bags, 3 clear turquoise bags, 3 plaid yellow bags."
--> function should return
[pale lime, pale lime, pale lime, pale lime, plaid green, plaid green, plaid green, plaid green, plaid green, clear turquoise, clear turquoise, clear turquoise, plaid yellow, plaid yellow, plaid yellow]
here "faded turquoise" is not relevant so the function overall should skip over it- meaning in the arraylist which is returned "faded turquoise" will not appear st= "faded turquoise bags contain 3 mirrored silver bags."
--> function should return
[mirrored silver bags, mirrored silver bags, mirrored silver bags]
here "drab magenta" is not relevant so the function overall should skip over st= "drab magenta bags contain 2 clear tomato bags. "
--> function should return
[clear tomato, clear tomato]
and so on when I try to put tostring()- newlist.add(list[i].split(" \s[0-9],\s ").toString()); --then the .add() is an error. how can I fix this when the listarray is string type array and get the "name color" it gives me the string and not the weird array hex string
help would be appreciated, help in improving the question or the clarity of my explanation is also welcome. thanks