Ahmad
Ahmad

Reputation: 72533

Replacing Strings in Java

I wanted to write a short programm, which replaces the Counting of a String. So I would like to start the String with 0 insted of 1. And Because it is a long String i dont want to change it all by my own. So the String (in this example) is: String LINE:

  1. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
  2. magna aliquyam erat, sed diam voluptua.
  3. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum
  4. dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
  5. magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
  6. no sea takimata sanctus est Lorem ipsum dolor sit amet.

And I want the String to start the counting with 0. and go on wit 1.2.3....(0,1,2,3,4...)

public static void main(String[] args) {
    for (int counter = 1; counter <= 300; counter++) {

        int NormCounter =1;
        int ReplaceCounter = 0;

        String NormCounterS  = (new Integer(NormCounter)).toString() + ".";

        String ReplaceCounterS = (new Integer(ReplaceCounter)).toString() + ".";
        Line = Line.replace(NormCounterS , ReplaceCounterS);
        ++ReplaceCounter;

        ++NormCounter;
    }

    System.out.println(Line);
}

it just changes the first "1." into "0."... So its 0,2,3,4... But i want the counting to go 0,1,2,3,4

Upvotes: 0

Views: 317

Answers (3)

Mike Clark
Mike Clark

Reputation: 10136

Even though you increment your counters, you re-set the counters to 1 and 0 every time the loop iterates. You should probably move this code:

int NormCounter = 1;
int ReplaceCounter = 0;

To outside the for-loop:

public static void main(String[] args) {

    int NormCounter = 1;
    int ReplaceCounter = 0;

    for (int counter = 1; counter <= 300; counter++) {
        String NormCounterS  = NormCounter + ".";
        String ReplaceCounterS = ReplaceCounter + ".";

        Line = Line.replace(NormCounterS, ReplaceCounterS);

        ++ReplaceCounter;
        ++NormCounter;
    }

    System.out.println(Line);
}

Also notice how String NormCounterS = (new Integer(NormCounter)).toString() + "."; can be rewritten more simply String NormCounterS = NormCounter + ".";. (The end result is the same).

See Laky's comment for an additional bug fix to this method.

Also just a small plug for Java coding conventions: it is standard to name Java variables with a lower case starting letter. E.g. use normCounter instead of NormCounter.

Upvotes: 8

Laky
Laky

Reputation: 965

As others suggested, place the variables assignment outside the for loop. However, your code will still not work, you will match "31." in the first iteration and change it to "30." and then in the later iteration, you will match "30." and change it to "29.", so you will in fact change "31." to "29.", not "30." as you wanted to. Try the following: (I assume there is a space in front of the numbers)

for (int counter = 0; counter < 300; counter++) {
    line = line.replace(" " + (counter + 1) + ".", " " + counter + ".");
}

EDIT: Probably a nicer solution would be to use replaceFirst and no assumptions are needed this time:

for (int counter = 0; counter < 300; counter++) {
    line = line.replaceFirst((counter + 1) + ".", counter + ".");
}

That should do the trick for now.

Note: it is conventional to start the variable names with a lower case letter and use names starting with a capital for class names.

Upvotes: 2

GingerHead
GingerHead

Reputation: 8230

place the int NormCounter =1; int ReplaceCounter = 0; outside the for loop

Upvotes: 0

Related Questions