I’m writing a program that add a substring to a black or a white string based on 1 main string.
The mainstring is: milk,paper,cup:,coffee,cola,PC
I want the code to place all the character before the “:” in the white string and all the characters after the “:” in the black string.
The problem is that it adds all the items to the black string except for the one witch is connected to the “:”. So in this case “cup”.
This is my code:
String White = "";
String Black = "";
String[] temp = "milk,paper,cup:,coffee,cola,PC".split(",");
int q = 0;
Boolean black = false;
while (q < temp.length) {
if (temp[q].isEmpty()) {
} else if (temp[q].contains(":")) {
String tempWhite = White;
White = tempWhite + temp[q].replace(":", "") + ",";
black = true;
} else if (black = true) {
String tempBlack = Black;
Black = tempBlack + temp[q] + ",";
} else if (black = false) {
String tempWhite = White;
White = tempWhite + temp[q] + ",";
} else if (temp[q].contains(" ")) {
} else {
System.out.println(temp[q]);
}
q++;
}
System.out.println("White: " + White);
System.out.println("Black: " + Black);
Output I get:
White: cup,
Black: milk,paper,coffee,cola,PC,
Output I want:
White: milk,paper,cup
Black: coffee,cola,PC
I made the script compilable for easy checking 🙂
Greetings and Thanks in advance,
Bram
split on “:” : [0] will be White, [1] Black .
Otherwise, use StringBuilder(), and method .append(..)