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\\yob2003.txt");
- //Make the Scanner read the file
- Scanner fileScanner = new Scanner(text);
- //Calculate the number of lines in the file
- int numLines = 0;
- while( fileScanner.hasNextLine() ){
- fileScanner.nextLine();
- numLines++;
- }
- //Reset fileScanner by making it a new object again
- fileScanner = new Scanner(text);
- //Make an array of Name objects of length numLines
- Name[] names = new Name[ numLines ];
- //Loop over all lines and store the ith Name object into spot i in array names
- for( int i = 0; i < numLines; i++ ){
- String line = fileScanner.nextLine();
- Scanner lineScanner = new Scanner( line );
- lineScanner.useDelimiter(",");
- String name = lineScanner.next();
- String gender = lineScanner.next();
- int freq = lineScanner.nextInt();
- Name n = new Name(name, gender, freq);
- names[i] = n;
- }
- System.out.println( "Longest Name" );
- System.out.println( longestName( names ) );
- System.out.println( "\nNames with no Vowels" );
- printNoVowels( names );
- System.out.println( "\nPalindromic Names" );
- printPalindromes( names );
- }
- public static String longestName( Name[] names ){
- int maxLength = 0;
- String longName = "";
- //Loop through the array of names and if you find a name that is longer
- //than longName, update maxLength and longName
- for(int i = 0; i < names.length; i++){
- Name n = names[i];
- if( n.getName().length() > maxLength && n.getGender().equals("F") ){
- maxLength = n.getName().length();
- longName = n.getName();
- }
- }
- return longName;
- }
- //Print names with no vowels
- public static void printNoVowels( Name[] names ){
- //Make an array of all the vowels
- String[] vowels = {"a", "e", "i", "o", "u"};
- //Loop over all Name objects in the array
- for(int i = 0; i < names.length; i++){
- boolean hasVowels = false;
- //get each individual name from each Name object
- String name = names[i].getName();
- //Loop over all the vowels and if the name contains any one of the vowels, indicate it has vowels in variable hasVowels
- for(int j = 0; j < vowels.length; j++){
- if( name.toLowerCase().contains( vowels[j] ) ) hasVowels = true;
- }
- //If hasVowels is still false meaning the name has no vowels, print the name
- if( !hasVowels ) System.out.println( name );
- }
- }
- //Method to print all palindromic (same forwards and backwards)
- public static void printPalindromes( Name[] names ){
- //Informed way - reverse the name and if name reversed equals name forwards, print it
- //Loop over names array
- for(int i = 0; i < names.length; i++){
- String name = names[i].getName().toLowerCase();
- String backwards = "";
- //Loop over characters of name backwards and store them in result
- for(int j = name.length() - 1; j >= 0; j--){
- backwards += name.substring(j, j+1);
- }
- //if name is equal to backwards name, print it
- if( name.equals(backwards) ) System.out.println( names[i].getName() );//print this instead of name so capit maintained
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement