How to fix "No new line at end of file" ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noralyn Nguyen
    New Member
    • May 2020
    • 1

    How to fix "No new line at end of file" ?

    Case 1:
    Code:
    import java.util.Scanner;
    
    public class PoD {
    	public static void main (String [] args) {
                Scanner in = new Scanner(System.in);
    		int number1 = in.nextInt();
    		int number2 = in.nextInt();
    		int number3 = in.nextInt();
    		
    	double[] arr = {2,3,4};
            double total = 0;
    
            for(int i=0; i<arr.length; i++){
            	total = total + arr[i];
            }
            double average = total / arr.length;
    	System.out.printf("Average number = %.2f", average);
    	}
    }
    Case 2:
    Code:
    public class average {
      public static void main(String[] args) {
      double[] arr = {16,18,19};
            double total = 0;
    
            for(int i=0; i<arr.length; i++){
            	total = total + arr[i];
            }
            double average = total / arr.length;
    	System.out.printf("Average number = %.3f",average);
    		}
    }
    UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT

    1c1
    < Average number = 3.00
    \ No newline at end of file
    ---
    > Average number = 17.67
    \ No newline at end of file
    What does it mean ? How to fix it ?
    Last edited by gits; May 16 '20, 11:48 PM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    Unix doesn't treat a file as a text file if there's no newline at the end of the file. This warning is shown to avoid issues while working with multiple files. Say there are two files. The first one with no newline at the end. On passing both files as arguments, the last line of the first file may add up with the first line of the second file on performing the concatenation. Therefore it's always good to use '\n'/CR/CRLF.

    POSIX IEEE standards: https://pubs.opengroup.org/onlinepub...tml#tag_03_206

    See my older related answer: https://bytes.com/topic/c/answers/97...le#post3824415

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      Simply place a newline that is required at the end of a source file to not mess up things. Here's what happens: The #include preprocessor directive adds the content of the included file to the current file as it is

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 656

        #4
        And what's the point of copying-and-pasting the text from one of my previous answers when I've already provided the link?

        Comment

        Working...