Reputation: 91
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
Upvotes: 1
Views: 138
Reputation: 1264
split on ":" : [0] will be White, [1] Black .
Otherwise, use StringBuilder(), and method .append(..)
Upvotes: 0
Reputation: 11369
The main problem why the shown code doesn't work is the wrong usage of =, the assignment operator, in the if conditions (when in fact, ==, the comparison operator, should be used - but you don't even need to compare at all - the variable black
itself is boolean, and can therefore be used as condition, e.g.:
if (black)
to execute something if black is true, or
if (!black)
to check whether black currently holds the value false
.
Note also, that in this code
if (black) {
doSomething();
} else if (!black) {
doSomethingElse();
}
the second if statement (if (!black)
) is superfluous: black
can only have two values; if it's not true, it can only be false! So a semantically equivalent to the above would be:
if (black) {
doSomething();
} else {
doSomethingElse();
}
As for an easier-to-read (better maintainable, and much shorter!) solution to the overall problem you are facing, use split on the colon first (as noted in the other answers already).
Upvotes: 0
Reputation: 1138
you can use the split method if you want to make a part of you string.you can split the string by this way(). split(":") and the store the pair of string in different string variable..thats all
Upvotes: 0
Reputation: 15357
Upvotes: 0
Reputation: 2415
You could just as easily split the main string at the ":"
Then you would have to remove the leading "," from the black string
Upvotes: 0
Reputation: 9260
How about
String[] pairs = "milk,paper,cup:,coffee,cola,PC".split(":");
String white = pairs[0];
String black = pairs[1].substring(1);
for a solution.
Upvotes: 3
Reputation: 200148
It seems like you have started out wrong: why don't you first split
by ":" and analyze the two parts afterwards? Your entire program logic will then collapse into two-three lines of code.
Upvotes: 5