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);
}
gg-14
  • 67
  • 5
  • Is the rest of the string (AAA, BBB and so on) is constant, or does it change? – TDG Aug 26 '15 at 16:31
  • You can use String.format – yas Aug 26 '15 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)` – Pshemo Aug 26 '15 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. – gg-14 Aug 26 '15 at 17:02

4 Answers4

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.

Community
  • 1
  • 1
Mifeet
  • 12,949
  • 5
  • 60
  • 108
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.

yas
  • 3,520
  • 4
  • 25
  • 38
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);
    }
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
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)));
}
Andreas
  • 154,647
  • 11
  • 152
  • 247