Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Necessary imports
- import java.util.Scanner;//Allows us to use the Scanner class
- import java.io.File;//Allows us to use the File class to create and Scan a File
- import java.io.FileNotFoundException;//It is possible the File class gives an error that we need to handle
- public class NameRunner{
- //Don't worry about the throws part, not on the AP test, necessary to handle aforementioned error
- public static void main(String[] args) throws FileNotFoundException{
- //Create a file using the directory and file you downloaded, YOUR directory may be different
- File text = new File("C:\\Users\\FOSTERBL\\Downloads\\yob2008.txt");
- //Make the Scanner read the file
- Scanner fileScanner = new Scanner(text);
- //Keep going while the file has another line
- while( fileScanner.hasNextLine() ){
- //Use a different Scanner to process each individual line using commas
- Scanner lineScanner = new Scanner( fileScanner.nextLine() );
- lineScanner.useDelimiter(",");
- //"break off" each individual data piece from a line using the line scanner
- String name = lineScanner.next();
- String gender = lineScanner.next();
- int freq = lineScanner.nextInt();
- //Instantiate and print a Name object using the information above for instance variables
- Name n = new Name( name, gender, freq );
- System.out.println( n );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement