// Simple File Input and Output using "new" I/O // Daniel Shiffman // Programming from A to Z, Spring 2006 // Based off of code from Java Regular Expressions by Mehran Habibi // Input file is the first argument passed from the command line // Output file is the second // This could be improved with some basic error handling (what if an invalid filename is entered, etc.?) import java.io.*; import java.nio.*; import java.nio.channels.*; import java.util.*; public class WordUpNew { static ArrayList xLyrics = new ArrayList(); public static void main (String[] args) throws IOException { int totalSentences = 0; String sentenceDelim = ":"; splitLyrics("JayZ-99Problems.txt", "Jay"); splitLyrics("DeLaSoul-TheGrindDate.txt", "Dela"); splitLyrics("KanyeWest-HeardEmSay.txt", "Kanye"); splitLyrics("FatBoys-AllYouCanEat.txt", "Fattie"); System.out.println("\n\n**************************************************"); System.out.println("** **"); System.out.println("** WORD UP - A DIALOGUE IN HIP HOP **"); System.out.println("** **"); System.out.println("**************************************************\n\n"); for(int i = 0; i < 20; i++ ) { int randomNum = (int) (Math.random() * xLyrics.size()); System.out.println(xLyrics.get(randomNum) + "\n"); } System.out.println("WORD!!!"); } public static void splitLyrics(String theFileName, String theArtistName) throws IOException { int sentences = 0; FileInputStream fis = new FileInputStream(theFileName); FileChannel fc = fis.getChannel(); ByteBuffer bb = ByteBuffer.allocate((int)fc.size()); fc.read(bb); fc.close(); // Convert ByteBuffer to one long String String content = new String(bb.array()); String regex = ":"; String[] Lyrics = content.split(regex); String lyricLine; for(int i = 0; i < Lyrics.length; i++ ) { lyricLine = Lyrics[i]; regex = "bitch"; // lyricLine = lyricLine.replaceAll(regex, "female dog"); regex = "nigga"; // lyricLine = lyricLine.replaceAll(regex, "brotha"); regex = "shit"; // lyricLine = lyricLine.replaceAll(regex, "doo doo"); regex = "ass"; // lyricLine = lyricLine.replaceAll(regex, "bootie"); xLyrics.add(theArtistName + ": " + lyricLine); //xLyrics.add(Lyrics[i]); } } public static int countSyllables(String word) { int syl = 0; boolean vowel = false; int length = word.length(); //check each word for vowels (don't count more than one vowel in a row) for(int i=0; i < length ; i++) { if (isVowel(word.charAt(i)) && (vowel==false)) { vowel = true; syl++; } else if (isVowel(word.charAt(i)) && (vowel==true)) { vowel = true; } else { vowel = false; } } char tempChar = word.charAt(word.length()-1); //check for 'e' at the end, as long as not a word w/ one syllable if (((tempChar == 'e') || (tempChar == 'E')) && (syl != 1)) { syl--; } return syl; } //check if a char is a vowel (count y) public static boolean isVowel(char c) { if ((c == 'a') || (c == 'A')) { return true; } else if ((c == 'e') || (c == 'E')) { return true; } else if ((c == 'i') || (c == 'I')) { return true; } else if ((c == 'o') || (c == 'O')) { return true; } else if ((c == 'u') || (c == 'U')) { return true; } else if ((c == 'y') || (c == 'Y')) { return true; } else { return false; } } }