Bram
Bram

Reputation: 91

String check error

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

Answers (7)

cl-r
cl-r

Reputation: 1264

split on ":" : [0] will be White, [1] Black .

Otherwise, use StringBuilder(), and method .append(..)

Upvotes: 0

codeling
codeling

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

sachit
sachit

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

Michel Keijzers
Michel Keijzers

Reputation: 15357

  • First, split on : to get two strings, one for the white string, one for the black string.
  • Then for both strings, split on , to select each separate item.
  • Use (boolean == true) in stead of (boolean = false)

Upvotes: 0

LuigiEdlCarno
LuigiEdlCarno

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

nullpotent
nullpotent

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

Marko Topolnik
Marko Topolnik

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

Related Questions