2

For my class, we have to make a program that takes user input and prints that many numbers of the fibonacci sequence. I managed to do that, but my professor wants it in a field width of 15 and justified to the left. For some reason I'm not understanding, it just will not give me the correct output. Here's what I have:

boolean valid = false;  
while (valid == false) {

    System.out.println("Please enter a valid number (between: 1 - 50):");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();

    if  (n < 1) {
       System.out.println("The number entered " + n + " is not valid");
    }
    else if (n > 50) {
       System.out.println("The number entered " + n + " is not valid");
    } 
    else if (n >= 1 || n <= 50) {
       valid = true;
       //System.out.printf("%15s", "1 ");
       int a = 0;
       int b = 1;
       for (int i = 1; i <= n; i++) {
           int nextNumber = a + b;
           if (i%5 != 0) {
               System.out.printf("%15s", nextNumber + " ");
           }
           else {
               System.out.printf("%15s", nextNumber + " " + "\n");
           }
           a = b;
           b = nextNumber;

The output is right justified, but when I make it "%-15s", the values do not all line up correctly and it's still right justified. Also, that commented out line is because I can't get the sequence to print out the first one.

River
  • 8,585
  • 14
  • 54
  • 67
  • 2
    If all you're asking about is text alignment then all the stuff in this question about the Fibonacci sequence is irrelevant – khelwood Oct 25 '17 at 21:05
  • The question does not seem to be a duplicate of the specified question, the OP knows about `-` modifier. – DAle Oct 25 '17 at 22:10

2 Answers2

2

The main problem in your code with %-15s format is this line:

System.out.printf("%-15s", nextNumber + " " + "\n");

You get the string, for example, "123 \n" and print it right-padded with spaces, but these spaces are added after the \n on the new line, and that creates all the mess (a possible way to fix this is to include \n into the format string: "%-15s\n"). What seems to be a right-justified text is actually a left-justified text with spaces at the beginning of the lines.

We need to fix that issue. Also, we could get rid of conversion to string, redundant spaces, separate processing of the first number:

int a = 0;
int b = 1;

for (int i = 0; i <= n; i++) {
    System.out.printf("%-15d", b); 
    if (i % 5 == 4) System.out.println();

    int nextNumber = a + b;
    a = b;
    b = nextNumber; 
} 
DAle
  • 8,990
  • 2
  • 26
  • 45
2

I don't think you need the spaces in your printf -- that is what the %-15 provides. Also, as an aside, you definitely don't need the boolean or the while loop. This seems to work for me:

public class Test { 

    public static void main(String[] args) {
        new Test().fib();
    }

    void fib() {
        System.out.println("Please enter a number between 1 and 50:");
        Scanner scan = new Scanner(System.in);
        final int n = scan.nextInt();

        if (n < 1 || n > 50) {
            System.out.println("The number entered is not valid");
            System.exit(0);
        }

        int a = 0;
        int b = 1;
        for (int i = 1; i <= n; i++) {
            int next = a + b;
            System.out.printf("%-15d", next);
            if (i % 5 == 0) System.out.println();
            a = b;
            b = next;
        }
    }

}

For example:

Please enter a number between 1 and 50:
45
1              2              3              5              8              
13             21             34             55             89             
144            233            377            610            987            
1597           2584           4181           6765           10946          
17711          28657          46368          75025          121393         
196418         317811         514229         832040         1346269        
2178309        3524578        5702887        9227465        14930352       
24157817       39088169       63245986       102334155      165580141      
267914296      433494437      701408733      1134903170     1836311903 
Blake
  • 986
  • 9
  • 24