0

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

3
  • I am having difficulty understanding what you are trying to accomplish, can you restate the question more concisely? "which replaces the Counting of a String" What does that mean? Why are you reinitializing your counters every loop? Commented Apr 6, 2012 at 23:46
  • @wcdolphin: ^_^ Even I had some difficulty. I guess all he wants is to start the numbering of the lines from 0 and not from 1. Commented Apr 6, 2012 at 23:47
  • 1
    There's still an error with your code with numbers that have more than 1 digit. E.g. you can get "31." changed to "29." and "123." to "120.". See my answer for further explanation. Commented Apr 7, 2012 at 0:11

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@user1253575: In the future I would recommend dry running your code before you waste time typing your question here. It would help you in most cases.
Note that there is still another error with the code. You should try to match for " " + NormCounter + ".". See my answer for further explanation and more concise way of how to write this.
@Laky: Indeed. I will link to your answer from mine.
2

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.