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.

    public static void main(String[] args) {


    int Counter = 1;

    while (Counter <=300){
        int counter2 =1;
        String Counterstring ;
        Counterstring = (new Integer(counter2)).toString() + ".";
        int ReplaceCounting = 0;
        String Replace = (new Integer(ReplaceCounting)).toString() + ".";
        Line.replace(Counterstring , Replace);

        Counter++;

    }

    System.out.println(Line);

}

}

Can somebody tell me what I did wrong? The output is just the same.

EDIT:

I changed it into: I changed it in:

   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);

}

}

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

2 Answers 2

5

You're assuming that strings are mutable. They're not. Calling replace and then ignoring the return value will do nothing useful. You want:

Line = Line.replace(Counterstring, Replace);

Although for preference, you should use camelCase for variable names, and avoid doing a lot of work for no reason. For example, your loop would be better as:

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

It's not clear why you'd want to do it multiple times, of course... your description is pretty obscure to me, I'm afraid.

EDIT: With your edited code, it looks like you really want:

for (int counter = 1; counter <= 300; counter++) {
    line = line.replace(counter + ".", (counter - 1) + ".");
}
Sign up to request clarification or add additional context in comments.

4 Comments

I changed it in: public static void main(String[] args) { int Counter = 1; while (Counter <=300){ int counter2 =1; String Counterstring ; Counterstring = (new Integer(counter2)).toString() + "."; int ReplaceCounting = 0; String Replace = (new Integer(ReplaceCounting)).toString() + "."; Line = Line.replace(Counterstring , Replace); ++ReplaceCounting; ++Counter; ++counter2; } System.out.println(Line); } } But it still changes the first "1." into "0."... So its 0,2,3,4... But i want the counting to go 0,1,2,3,4
@user1253575 Don't paste code into comments. Add an update to your question instead.
@user1253575: I see you've completely ignored the advice about variable names... and you're still creating new Integer objects for no reason. The problem now is that you're starting NormCounter and ReplaceCounter again on each iteration of the loop. I've edited my answer - if that doesn't work, please show a short but complete example of it failing.
ah I see now.. your right, it would be better with your code. Thanks :)
2

First off, like Jon said, not having proper camelCase for your variables makes your code almost unreadable for regular Java users. Any new code you post should really use the established conventions.

Now, as for your code, you're resetting your counters every time you iterate through the loop. So every time you get to line.replace(normCounter, replaceCounter) it's doing line.replace("1.", "0.")

Here's what you should have done:

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

System.out.println(line);

Notice how I initialized my counter outside of my for-loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.