Advertisement
fosterbl

NameRunner.java with all methods so far

Dec 5th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 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.          String name = lineScanner.next();
  34.          String gender = lineScanner.next();
  35.          int freq = lineScanner.nextInt();
  36.          Name n = new Name(name, gender, freq);
  37.          names[i] = n;
  38.       }
  39.      
  40.       System.out.println( "Longest Name" );
  41.       System.out.println( longestName( names ) );
  42.      
  43.       System.out.println( "\nNames with no Vowels" );
  44.       printNoVowels( names );
  45.      
  46.       System.out.println( "\nPalindromic Names" );
  47.       printPalindromes( names );
  48.    }
  49.    
  50.    public static String longestName( Name[] names ){
  51.       int maxLength = 0;
  52.       String longName = "";
  53.       //Loop through the array of names and if you find a name that is longer
  54.       //than longName, update maxLength and longName
  55.       for(int i = 0; i < names.length; i++){
  56.          Name n = names[i];
  57.          if( n.getName().length() > maxLength && n.getGender().equals("F") ){
  58.             maxLength = n.getName().length();
  59.             longName = n.getName();
  60.          }
  61.       }
  62.       return longName;
  63.    }
  64.    
  65.    //Print names with no vowels
  66.    public static void printNoVowels( Name[] names ){
  67.       //Make an array of all the vowels
  68.       String[] vowels = {"a", "e", "i", "o", "u"};
  69.       //Loop over all Name objects in the array
  70.       for(int i = 0; i < names.length; i++){
  71.          boolean hasVowels = false;
  72.          //get each individual name from each Name object
  73.          String name = names[i].getName();
  74.          //Loop over all the vowels and if the name contains any one of the vowels, indicate it has vowels in variable hasVowels
  75.          for(int j = 0; j < vowels.length; j++){
  76.             if( name.toLowerCase().contains( vowels[j] ) ) hasVowels = true;
  77.          }
  78.          //If hasVowels is still false meaning the name has no vowels, print the name
  79.          if( !hasVowels ) System.out.println( name );
  80.       }
  81.    }
  82.    
  83.    //Method to print all palindromic (same forwards and backwards)
  84.    public static void printPalindromes( Name[] names ){
  85.       //Informed way - reverse the name and if name reversed equals name forwards, print it
  86.      
  87.       //Loop over names array
  88.       for(int i = 0; i < names.length; i++){
  89.          String name = names[i].getName().toLowerCase();
  90.          String backwards = "";
  91.          
  92.          //Loop over characters of name backwards and store them in result
  93.          for(int j = name.length() - 1; j >= 0; j--){
  94.             backwards += name.substring(j, j+1);
  95.          }
  96.          
  97.          //if name is equal to backwards name, print it
  98.          if( name.equals(backwards) ) System.out.println( names[i].getName() );//print this instead of name so capit maintained
  99.       }
  100.    }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement