Advertisement
fosterbl

NameRunner.java - 3rd Hour

Dec 5th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 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\\yob2003.txt");
  12.       //Make the Scanner read the file
  13.       Scanner fileScanner = new Scanner(text);
  14.      
  15.       //Calculate the number of lines in the file
  16.       int numLines = 0;
  17.       while( fileScanner.hasNextLine() ){
  18.          fileScanner.nextLine();
  19.          numLines++;
  20.       }
  21.      
  22.       //Reset fileScanner by making it a new object again
  23.       fileScanner = new Scanner(text);
  24.      
  25.       //Make an array of Name objects of length numLines
  26.       Name[] names = new Name[ numLines ];
  27.      
  28.       //Loop over all lines and store the ith Name object into spot i in array names
  29.       for( int i = 0; i < numLines; i++ ){
  30.          String line = fileScanner.nextLine();
  31.          Scanner lineScanner = new Scanner( line );
  32.          lineScanner.useDelimiter(",");
  33.          Name n = new Name( lineScanner.next(), lineScanner.next(), Integer.parseInt( lineScanner.next() ) );
  34.          names[i] = n;
  35.       }
  36.      
  37.       System.out.println( "Longest Name" );
  38.       System.out.println( longestName( names ) );
  39.      
  40.       System.out.println( "\nNames with no Vowels" );
  41.       printNoVowels( names );
  42.      
  43.       System.out.println( "\nPalindromic Names" );
  44.       printPalindromes( names );
  45.    }
  46.    
  47.    public static String longestName( Name[] names ){
  48.       int maxLength = 0;
  49.       String longName = "";
  50.       //Loop through the array of names and if you find a name that is longer
  51.       //than longName, update maxLength and longName
  52.       for(int i = 0; i < names.length; i++){
  53.          Name n = names[i];
  54.          if( n.getName().length() > maxLength && n.getGender().equals("F") ){
  55.             maxLength = n.getName().length();
  56.             longName = n.getName();
  57.          }
  58.       }
  59.       return longName;
  60.    }
  61.    
  62.    //Print names with no vowels
  63.    public static void printNoVowels( Name[] names ){
  64.       //Make an array of all the vowels
  65.       String[] vowels = {"a", "e", "i", "o", "u"};
  66.       //Loop over all Name objects in the array
  67.       for(int i = 0; i < names.length; i++){
  68.          boolean hasVowels = false;
  69.          //get each individual name from each Name object
  70.          String name = names[i].getName();
  71.          //Loop over all the vowels and if the name contains any one of the vowels, indicate it has vowels in variable hasVowels
  72.          for(int j = 0; j < vowels.length; j++){
  73.             if( name.toLowerCase().contains( vowels[j] ) ) hasVowels = true;
  74.          }
  75.          //If hasVowels is still false meaning the name has no vowels, print the name
  76.          if( !hasVowels ) System.out.println( name );
  77.       }
  78.    }
  79.    
  80.    //Method to print all palindromic (same forwards and backwards)
  81.    public static void printPalindromes( Name[] names ){
  82.       //Informed way - reverse the name and if name reversed equals name forwards, print it
  83.      
  84.       //Loop over names array
  85.       for(int i = 0; i < names.length; i++){
  86.          String name = names[i].getName().toLowerCase();
  87.          String backwards = "";
  88.          
  89.          //Loop over characters of name backwards and store them in result
  90.          for(int j = name.length() - 1; j >= 0; j--){
  91.             backwards += name.substring(j, j+1);
  92.          }
  93.          
  94.          //if name is equal to backwards name, print it
  95.          if( name.equals(backwards) ) System.out.println( names[i].getName() );//print this instead of name so capit maintained
  96.       }
  97.    }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement