Getting a extra '-' after rearranging the code snippets

Viewed 72

So I started learning java from the Head First Java book and stumbled upon a exercise.I need to rearrange these code snippets to obtain a output like this:

a-b c-d

The code snippets are:

if (x == 1) {
                System.out.print("d");
                x = x - 1
            }
if (x == 2) {
                System.out.print("b c");
            }
if (x > 2) {
                System.out.print("a");
           }
while (x > 0) {
x = x - 1;
System.out.print("-");
int x = 3;

So I did something like this:

public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x == 2) {
                System.out.print("b c");
            }
            if (x > 2) {
                System.out.print("a");
            }
            if (x == 1) {
                System.out.print("d");
            }
            x = x - 1;
            System.out.print("-");
        }
        
    }

}

The output I am getting is :

a-b c-d-

What did I do wrong

3 Answers

You missed one x = x - 1; in one of the if statements, and put the print statement in the wrong place:

public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x == 2) {
                System.out.print("b c");
            }
            if (x > 2) {
                System.out.print("a");
            }
            x = x - 1;
            System.out.print("-");
            if (x == 1) {
                System.out.print("d");
                x = x - 1;
            }
        }

    }
}
public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x == 2) {
                System.out.print("b c");
            }
            if (x > 2) {
                System.out.print("a");
            }
            if (x == 1) {
                System.out.print("d");
            }
            x = x - 1;
            System.out.print("-"); // will run for every iteration of the loop
        }
        
    }

}

Looking at your code here, after each iteration of the loop regardless of the value of x, it will always print a dash after the output. You are also missing x = x - 1; from

if (x == 1) {
                System.out.print("d");
                x = x - 1; // you were missing this
            }

The if statement above should also go below

x = x - 1;
System.out.print("-");

so that we don't add an unnecessary - at the end by setting x == 1 before the condition is checked so we don't go through another iteration.

Putting this all together we get this

public class cc {
    public static void main(String [] args) {
        int x = 3;
        while (x > 0) {
            if (x > 2) {
                System.out.print("a");
            }
            if (x == 2) {
                System.out.print("b c");
            }
            x = x - 1;
            System.out.print("-");
            if (x == 1) {
                System.out.print("d");
                x = x - 1;
            }
        }

    }
}

EDIT: I also rearranged the if statements for you as > 2 coming before == 2 makes more logical sense

The point of the exercise is to understand loops (in this case a while loop), if statements and modifying the value stored in a variable. To that end I would suggest also looking at arrays and trying to generate the desired output. For example this specific case will print a-b c-d but what's the general case? If you have a bunch of characters it looks like they are to be broken into pairs where each pair is separated by a space and between each element of any given pair there is a hyphen.

So supposing you have

String input = "abcd";

What do you have to write to get

a-b c-d

In output?

One possibility is the following

char[] chars = input.toCharArray();
int i = 0;
while (i < chars.length) {
    String separator;
    System.out.print(chars[i]);
    if (i == chars.length - 1) {
        separator = "\n";
    else if (i % 2 != 0) {
        separator = " ";
    } else {
        separator = "-";
    }
    System.out.print(separator);
    i++;
}
Related