2

For some reason my loop only outputs the "\t" after the first iteration of my for-loop. Here is my loop's code:

input = -1;
private String[] types = {"none", "vanilla", "hazelnut", "peppermint", "mocha", "caramel"};
while ( (input < 0) || (input > (types.length - 1)) ){ //gets user's latte flavor input
        System.out.println("Enter the number corresponding to the latte type you would like:");
        for ( int i = 0; i < types.length; i++ ){
            if ( i <= (types.length - 2) ){ //prints two options per line
                System.out.println(i + ": " + types[i] + "\t" + (i + 1) + ": " + types[i + 1]);
            }
            else if ( i == (types.length - 1) ){
                System.out.println(i + ": " + types[i]);
            }
            else{ //does nothing on odd indices
            }
            i++;
        }
        input = keyboard.nextInt();
    }

This outputs the following:

Enter the number corresponding to the latte type you would like:
0: none     1: vanilla
2: hazelnut     3: peppermint
4: mocha        5: caramel

As we can see, "1: vanilla" is not spaced in the same way the other rows are. My code for my Tea class, however, works properly:

input = -1;
private String[] types = {"white", "green", "oolong", "black", "pu-erh", "camomille"};
while ( (input < 0) || (input > (types.length - 1)) ){ //gets user's tea flavor input
        System.out.println("Enter the number corresponding to the tea type you would like:");
        for ( int i = 0; i < types.length; i++ ){
            if ( i <= (types.length - 2) ){ //prints two options per line
                System.out.println(i + ": " + types[i] + "\t" + (i + 1) + ": " + types[i + 1]);
            }
            else if ( i == (types.length - 1) ){
                System.out.println(i + ": " + types[i]);
            }
            else{ //does nothing on odd indices
            }
            i++;
        }
        input = keyboard.nextInt();
    }

And this outputs the following:

Enter the number corresponding to the tea type you would like:
0: white    1: green
2: oolong   3: black
4: pu-erh   5: camomille

What causes my Latte loop (my Espresso loop also suffers this spacing issue) to output differently than my Tea loop? Thanks for helping me understand this behavior!

shtuken
  • 171
  • 1
  • 9
  • I've suggested a `printf()` solution to your problem... this might be the easiest way to deal with your problem – ParkerHalo Dec 16 '15 at 09:33

7 Answers7

5

Well the TAB is actually there. Note that 0: none is one character shorter than the other examples you have posted. So you tab to an earlier tab stop.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • You might suggest a cure, maybe `String.format()` , `Formatter` or `PrintStream.printf()` – Paul Hicks Dec 16 '15 at 08:57
  • Thanks for the explanation. I was wondering why it seemed like the tab did not simply indent by a constant amount of space. – shtuken Dec 16 '15 at 18:14
3

Since there is none yet, i'll provide a solution using printf. You could just use a formatter (like System.out.printf() to format the string):

System.out.printf("%d: %-12s%d:%-12s\n", i, types[i], i+1, types[i+1]);
  • The %d allows you to input an Integer type.
  • The %-12s allows you to input a String (minimum length is 12 left aligned)... this replaces your tabs!
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
1

none is a small word compared to the others. You could pad the words in the types array with spaces in the end to have the same length in order to solve this issue.

gon1332
  • 1,930
  • 1
  • 24
  • 30
1

Tabs are tricky since they are affected by position on line, and tab width.

Better use padding by spaces. A convenient way using Apache Commons:

StringUtils.rightPad(types[i], COLUMN_WIDTH)

(adjust COLUMN_WIDTH based on your longest text)

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
0

Following is a fix with format

System.out.println(i + ": " + String.format("%-5s\t", types[i]) + (i + 1) + ": " + types[i + 1]);

This will fix the issue.

Thanga
  • 7,811
  • 3
  • 19
  • 38
-1

This is caused because of the length of the first String in your array. Try it with "nonee" instead of "none" and see.

If you want to do it properly you should use some sort of padding.

Dagan Sandler
  • 475
  • 1
  • 5
  • 16
-1

The tab exists as mentioned above. As to why things are not aligned. You can refer to this link

Community
  • 1
  • 1
maximus
  • 19
  • 2