1

I have a String with digits at index 4 and 18. I need to print the String to the Java console such that the number increments by one until it reaches the number 40.

String:

String a = "AAA_0_BBB_CCC(DDD_0_EEE)" ;

Desired Output in Console:

AAA_1_BBB_CCC(DDD_1_EEE)
AAA_2_BBB_CCC(DDD_2_EEE)
AAA_3_BBB_CCC(DDD_3_EEE)
.
.
.
AAA_40_BBB_CCC(DDD_40_EEE)

My program (see below) prints digits from 0 to 9. But I didn't know how to continue the loop with double digits (11 to 40). I can rewrite the program for each index(4,5,19,20) but i was wondering if there was a simpler way to handle it.

My program for changing at index 4 and 18 with one digit:

public static void main(String[] args){
StringBuffer buf = new StringBuffer( a );
String a = "AAA_0_BBB_CCC(DDD_0_EEE)" ;
for (int i = 0; i< 10; i++){
//make i a character j
String k = Integer.toString(i);
char j = k.charAt(0);
//replace index position 4 and 18
buf.setCharAt( 4, j );
buf.setCharAt( 18, j );
String z = buf.toString( );
System.out.println(z);
}
4
  • Is the rest of the string (AAA, BBB and so on) is constant, or does it change? Commented Aug 26, 2015 at 16:31
  • You can use String.format Commented Aug 26, 2015 at 16:31
  • Are both numbers guaranteed to be same or is input like AAA_0_BBB_CCC(DDD_2_EEE) possible? If it is possible then what should be final result? Should it be AAA_38_BBB_CCC(DDD_40_EEE) or AAA_40_BBB_CCC(DDD_42_EEE) Commented Aug 26, 2015 at 16:35
  • yes it changes, the actual string that I am working on is a long query in SQL that needs to be repeated. The string I used in this question was for ease of use. And the numbers has to be the same in each string. Thank you. Commented Aug 26, 2015 at 17:02

4 Answers 4

5

What you need can easily be achieved with String.format()

for (int i = 0; i<= 40; i++){
    String str = String.format("AAA_%d_BBB_CCC(DDD_%d_EEE)", i, i);
    System.out.println(str);
}

You could even simplify the call a little by using argument index:

String.format("AAA_%1$d_BBB_CCC(DDD_%1$d_EEE)", i);

In general, it is a good idea to use String.format() (or System.out.printf() as Andreas mentioned) whenever you need to create a string out of non-string values. Check out Formatter for documentation of the format string syntax.


One more thing: I see you are using StringBuffer in your code. Consider using StringBuilder which is slightly faster (and not thread-safe). Using StringBuilder, what you need to achieve could look like

String str = new StringBuilder().append("AAA_").append(i).append("_BBB_CCC(DDD_").append(i).append("_EEE)").toString();

Or you could even use simple

String str = "AAA_" + i + "_BBB_CCC(DDD_" + i + "_EEE)";

Java compiler is smart enough to insert StringBuilder where concatenating longer several strings automatically.

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

2 Comments

You can combine the lines: System.out.printf("AAA_%d_BBB_CCC(DDD_%d_EEE)%n", i, i);
Thank you. I didn't know about the format method in String. This is very helpful.
3

You can pass a format string and substituion parameters to System.out.printf function:

for( int i = 0; i <= 40; i++ ) {
    System.out.printf("AAA_%d_BBB_CCC(DDD_%<d_EEE)%n", i);
}

The %<d specification is using the prior argument, so you don't need to pass the parameter i twice. The %n is the platform-dependent newline.

1 Comment

and %n too, I was missing that :)
3

You can use the following logic for formatted output.

public static void main(String[] args){
    for(int i = 1; i<= 40; i++){
        System.out.printf("AAA_%d_BBB_CCC(DDD_%d_EEE)%n", i, i);
    }
}

1 Comment

Thank you, i wasn't aware of printf() either. This also works.
2

Using regular expressions is a good choice if the string is dynamic and the numeric places have a pattern.

String a = "AAA_0_BBB_CCC(DDD_0_EEE)";
for (int i = 1; i <= 40; i++) {
    System.out.println(a.replaceAll("_0_", "_" + i + "_"));
}

This can be optimized for performance:

String a = "AAA_0_BBB_CCC(DDD_0_EEE)";
Matcher m = Pattern.compile("_0_").matcher(a);
for (int i = 1; i <= 40; i++) {
    System.out.println(m.replaceAll("_" + i + "_"));
}

Can even be simplified if you're sure there are no 0 digits in he rest of the string.

String a = "AAA_0_BBB_CCC(DDD_0_EEE)";
Matcher m = Pattern.compile("0").matcher(a);
for (int i = 1; i <= 40; i++) {
    System.out.println(m.replaceAll(String.valueOf(i)));
}

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.