Advertisement
fosterbl

NameRunner.java - fifth hour

Dec 4th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. //Necessary imports
  2. import java.util.Scanner;//Allows us to use the Scanner class
  3. import java.io.File;//Allows us to use the File class to create and Scan a File
  4. import java.io.FileNotFoundException;//It is possible the File class gives an error that we need to handle
  5.  
  6. public class NameRunner{
  7.    //Don't worry about the throws part, not on the AP test, necessary to handle aforementioned error
  8.    public static void main(String[] args) throws FileNotFoundException{
  9.      
  10.       //Create a file using the directory and file you downloaded, YOUR directory may be different
  11.       File text = new File("C:\\Users\\FOSTERBL\\Downloads\\yob2008.txt");
  12.       //Make the Scanner read the file
  13.       Scanner fileScanner = new Scanner(text);
  14.      
  15.       //Keep going while the file has another line
  16.       while( fileScanner.hasNextLine() ){
  17.          //Use a different Scanner to process each individual line using commas
  18.            Scanner lineScanner = new Scanner( fileScanner.nextLine() );
  19.          lineScanner.useDelimiter(",");
  20.          
  21.          //"break off" each individual data piece from a line using the line scanner
  22.          String name = lineScanner.next();
  23.          String gender = lineScanner.next();
  24.          int freq = lineScanner.nextInt();
  25.          
  26.          //Instantiate and print a Name object using the information above for instance variables
  27.          Name n = new Name( name, gender, freq );
  28.          System.out.println( n );
  29.       }
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement