0

New to coding and trying to create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each manager should be written using method format. Then modify the program in to read the survey responses from numbers.txt. The responses should be read from the file using a Scanner. Use method nextInt to input one integer at a time from the file. The program should continue to read responses until it reaches the end of the file. The results should be output to the text file “output.txt.”

I have tried a few different things and I keep getting resource leak.

public class StudentPoll {
  public static void main( String[] args ) {
  // student response array (more typically, input at runtime)
  int[] responses = { 1, 2, 5, 4, 3, 5, 2, 1, 3, 3, 1, 4, 3, 3, 3, 2, 3, 3, 2, 14 };
  int[] frequency = new int[ 6 ]; // array of frequency counters

  // for each answer, select responses element and use that value
  // as frequency index to determine element to increment
  for ( int answer = 0; answer < responses.length; answer++ ) {
    try {
      ++frequency[ responses[ answer ] ];
    } // end try
    catch ( ArrayIndexOutOfBoundsException e ) {
      System.out.println( e );
      System.out.printf( "   responses[%d] = %d\n\n", answer, responses[ answer ] );
    } // end catch
  } // end for

  System.out.printf( "%s%10s\n", "Rating", "Frequency" );

  // output each array element's value
  for ( int rating = 1; rating < frequency.length; rating++ )
    System.out.printf( "%6d%10d\n", rating, frequency[ rating ] );
  } // end main
} // end class StudentPoll

/*
java.lang.ArrayIndexOutOfBoundsException: 14
responses[19] = 14
Rating    Frequency
1         3
2         4
3         8
4         2
5         2
*/ 

Tried the below just playing around and get the resource leak

package studentPoll;

//Fig. 7.8: Numbers.java
//Writing data to a sequential text file with class Formatter.
import java.io.FileNotFoundException;     
import java.lang.SecurityException;       
import java.util.Formatter;               
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;  
import java.util.Scanner;                 

public class Numbers {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.printf("%s%n%s%n? ", 
       "Enter 14 survey responses and enter each time:",
        "Enter end-of-file indicator to end input.");

   // open numbers.txt, output data to the file then close numbers.txt
   try (Formatter output = new Formatter("numbers.txt")) {
      while (input.hasNext()) { // loop until end-of-file indicator
         try {
            // output new record to file; assumes valid input
            output.format("%d %d %d %d %d %d %d %d %d %d %d %d %d %d", input.nextInt(),  
               input.next(), input.next(), input.nextDouble());
         } 
         catch (NoSuchElementException elementException) {
            System.err.println("Invalid input. Please try again.");
            input.nextLine(); // discard input so user can try again
         } 

         System.out.print("? ");
      }
   }
   catch (SecurityException | FileNotFoundException | 
      FormatterClosedException e) {
      e.printStackTrace();
      System.exit(1); // terminate the program
   }
} 
}
Jwaldon
  • 45
  • 6
  • Possible duplicate of [Resource leak: 'in' is never closed](https://stackoverflow.com/questions/12519335/resource-leak-in-is-never-closed) – FailingCoder Sep 21 '19 at 01:48

1 Answers1

0

Scanner input = new Scanner(System.in);

Warning: Resource leak: 'input' is never closed

Just call input.close(); at the end of your program.

FailingCoder
  • 757
  • 1
  • 8
  • 20