Skip to main content
spelling fixes.
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144

YouYour code is not good, because you have a lot of duplicate lines, you create a lot of strings during executethe execution of your loop ( because strings are imutableimmutable, + creates a new string object). You have a lot of if statmentsstatements which have to be evaluated and some other things.

An alternative solution is:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Needneed to add the commons-lang3 library to your build/classpath.

You code is not good, because you have a lot of duplicate lines, you create a lot of strings during execute your loop ( because strings are imutable + creates a new string object). You have a lot of if statments which have to be evaluated and some other things.

An alternative solution is:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

Your code is not good, because you have a lot of duplicate lines, you create a lot of strings during the execution of your loop ( because strings are immutable, + creates a new string object). You have a lot of if statements which have to be evaluated and some other things.

An alternative solution is:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you need to add the commons-lang3 library to your build/classpath.

added 186 characters in body
Source Link
Jens
  • 297
  • 3
  • 13

IfYou code is not good, because you can use commons-lang3have a lot of duplicate lines, you create a lot of strings during execute your loop ( because strings are imutable + creates a new string object). You have a lot of if statments which have to be evaluated and some other things.

An alternative solution is:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

If you can use commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

You code is not good, because you have a lot of duplicate lines, you create a lot of strings during execute your loop ( because strings are imutable + creates a new string object). You have a lot of if statments which have to be evaluated and some other things.

An alternative solution is:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

added 88 characters in body
Source Link
Jens
  • 297
  • 3
  • 13

If you can use commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

If you can use commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

If you can use commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }
}

StringUtils.repeat is a helper method, which will print a character n times. So I create a string with n times a X and then I do it same with o for the rest and concatenate it for output.

The solution makes your code shorter, more readable, and it uses well tested functions from third party distributor. Why invent the wheel again?

For this solution you Need to add the commons-lang3 library to your build/classpath.

added 1 character in body
Source Link
Jens
  • 297
  • 3
  • 13
Loading
documentation link, and I cleaned up spelling here and there
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading
deleted 2 characters in body
Source Link
Jens
  • 297
  • 3
  • 13
Loading
documentation link, and I cleaned up spelling here and there
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading
added 139 characters in body
Source Link
Jens
  • 297
  • 3
  • 13
Loading
added 188 characters in body
Source Link
Jens
  • 297
  • 3
  • 13
Loading
deleted 11 characters in body
Source Link
Jens
  • 297
  • 3
  • 13
Loading
Source Link
Jens
  • 297
  • 3
  • 13
Loading