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!