0

I've made a code where it take the first two numbers from the input to determine how to number the following lines. As an example

Input:

2 -1
#include <stdio.h>

int main()
{
    printf(
       "1 Line\n"
       "2 Number\n"
       "3 Inserter\n");

    return 0;
}

Expected Output:

 1 #include <stdio.h>
 2
 3 int main()
 4 {
 5     printf(
 6        "1 Line\n"
 7        "2 Number\n"
 8        "3 Inserter\n");
 9
10     return 0;
11 }

But for some reason my code is printing:

 1 
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     printf(
 7        "1 Line\n"
 8        "2 Number\n"
 9        "3 Inserter\n");
10 
11     return 0;
12 }

How can I go about this so that it skips the first line and print it as how it is expected?

This is my code:

public void run() {
    int y = this.input.nextInt();
    int x = this.input.nextInt();
    int z = 1;
      
    if (x == 0) {
      
      while (this.input.hasNextLine()) {
      
        System.out.printf("%0"+y+"d %s\n", z, this.input.nextLine());
        
        z++;
      }
    }
    
    else {
      
      while (this.input.hasNextLine()) {
        
        System.out.printf("%"+y+"d %s\n", z, this.input.nextLine());
        
        z++;
      }
    } 

  }

int y sets how many spaces/zeros are before the number int x determines if there should be 0s or spaces (example 001 or ' 1' (added the '' to show the spaces)) int z to raise the number each time it prints it

0 Answers0