Java Scanner auto close in try catch

Description

Java Scanner auto close in try catch

import java.util.Scanner; 

public class Main {
  public static void main(String[] args) throws Exception {
    // Create a File instance
    java.io.File file = new java.io.File("scores.txt");

    try (Scanner input = new Scanner(file);) {
      // Read data from a file
      while (input.hasNext()) {
        String firstName = input.next();
        String mi = input.next();
        String lastName = input.next();
        int score = input.nextInt();
        System.out.println(/*  ww  w  .j a  v  a2  s  .  c  o m*/
          firstName + " " + mi + " " + lastName + " " + score);
      }
    }
  }
}



PreviousNext

Related