Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //name: Liam Rathke date: 20 Sep
- import java.util.*;
- import java.io.*;
- import javax.swing.JOptionPane;
- public class PigLatin
- {
- public static void main(String[] args) throws IOException
- {
- //part_1_using_pig();
- part_2_using_piglatenizeFile();
- }
- public static void part_1_using_pig()
- {
- Scanner sc = new Scanner(System.in);
- while(true)
- {
- System.out.print("\nWhat word? ");
- String s = sc.next();
- if (s.equals("-1"))
- System.exit(0);
- String p = pig(s);
- System.out.println("***** " + p + " *****");
- }
- }
- public static String pig(String s)
- {
- boolean firstCharCapitalized = false; //sets the first character's capitalization to false
- if(!s.substring(0,1).equals(s.substring(0,1).toLowerCase())) //if the letter is not the same as the lower case letter
- {
- firstCharCapitalized = true; //it is capitalized
- //System.out.println("The first character is capitalized!");
- s = s.toLowerCase();
- //System.out.println("Lower case check: " + s);
- }
- String punctuationChars = ",.!?:"; //list of characters
- String punctuation = ""; //default punctuation at the end of a string
- for(int o = 0; o < punctuationChars.length(); o++) //runs through the punctuation for the last index of the word
- {
- if(punctuationChars.substring(o, o + 1).equals(s.substring(s.length() - 1, s.length()))) //last char equals one of the punctuations
- {
- punctuation = s.substring(s.length()-1, s.length()); //changes the punctuation string to the appropriate char
- s = s.substring(0, s.length()-1);
- //System.out.println("Punctuation detected: " + punctuation);
- }
- }
- String vowels = "aeiou"; //possible vowels
- if(s.substring(0,1).equals("y")) //for special y case
- {
- //System.out.println("Y case returned!");
- int yCount = 0;
- while(!vowels.contains(s.substring(yCount, yCount + 1)))
- yCount ++;
- //System.out.println("The yCount is equal to " + yCount);
- String yFinal = s.substring(yCount, s.length()) + s.substring(0, yCount) + "ay";
- return yFinal; //returns y case
- }
- for(int i = 0; i < vowels.length(); i++)
- {
- if(s.substring(0, 1).equals(vowels.substring(i, i+1)))
- {
- //System.out.println("There is a vowel at the start!");
- if(firstCharCapitalized == true)
- {
- s = s.substring(0,1).toUpperCase() + s.substring(1, s.length());
- //System.out.println("The first character in pig latin is now capitalized! Check: " + s);
- }
- return(s + "way" + punctuation);
- }
- }
- vowels = vowels + "y";
- String consonants = s;
- int numCons = 0;
- vowelTestLoop: for(int j = 0; j < consonants.length()-1; j++)
- {
- for(int p = 0; p < vowels.length(); p++)
- {
- if(s.substring(j, j+1).equals(vowels.substring(p, p+1)))
- {
- numCons = j - 1;
- //System.out.println("There are " + (numCons) + " consonants before the first vowel!");
- //System.out.println("Vowel Check! " + s.substring(j, j+1) + ", " + vowels.substring(p, p+1));
- break vowelTestLoop;
- }
- }
- numCons = 0;
- }
- int newnumCons = numCons + 1;
- int vowelTester = 0;
- //System.out.print("Vowels in string:");
- for(int u = 0; u < vowels.length(); u++)
- {
- if(s.contains("" + vowels.charAt(u)))
- {
- //System.out.print(" " + vowels.charAt(u));
- vowelTester++;
- }
- }
- //System.out.println("");
- //System.out.println("There are " + vowelTester + " total vowels!");
- if(vowelTester == 0)
- {
- return ("INVALID");
- }
- if((vowelTester == 1) && (vowels.contains("" + s.charAt(s.length()-1))))
- {
- return("" + s.charAt(s.length()-1) + s.substring(0, s.length() - 1) + "ay");
- }
- String firstCons = consonants.substring(0, newnumCons); //+ 1);
- String otherChars = consonants.substring(newnumCons, consonants.length());
- //System.out.println("The firstCons String is " + firstCons);
- //System.out.println("The otherChars String is " + otherChars);
- if(((firstCons.substring(firstCons.length() - 1, firstCons.length()).equals("q"))) && ((otherChars.substring(0, 1)).equals("u")))
- {
- //System.out.println("There is a q before the u!");
- String qAtStart = otherChars.substring(1, otherChars.length()) + firstCons + "uay";
- if(firstCharCapitalized == true)
- {
- qAtStart = qAtStart.substring(0,1).toUpperCase() + qAtStart.substring(1, qAtStart.length());
- //System.out.println("The first character in pig latin is now capitalized! Check: " + qAtStart);
- }
- return qAtStart;
- }
- if(firstCharCapitalized == true)
- {
- otherChars = otherChars.substring(0,1).toUpperCase() + otherChars.substring(1, otherChars.length());
- //System.out.println("The first character in pig latin is now capitalized! Check: " + s);
- }
- //System.out.println("Normal case detected!");
- return (otherChars + firstCons + "ay" + punctuation);
- }
- public static void part_2_using_piglatenizeFile() throws IOException
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("What filename? ");
- String filename = sc.next();
- Scanner inFile = new Scanner(new File(filename)); //PigLatin.txt
- piglatenizeFile( inFile, filename );
- System.out.println("Piglatin done!");
- sc.close();
- }
- /******************************
- * take in a filename, and creates a file that is the inputted file
- * fully piglatenized. The outputFile should be in piglatin form
- * PigLatin.txt should become IgLatinpay.txt.
- *
- * Note: filename will have .txt on it.
- ******************************/
- public static void piglatenizeFile(Scanner inFile, String filename) throws IOException
- {
- try{
- inFile = new Scanner(new File(filename));
- }
- catch(FileNotFoundException e)
- {
- JOptionPane.showMessageDialog(null,"The file could not be found.");
- System.exit(0);
- }
- PrintStream outFile = null;
- try{
- outFile = new PrintStream(new FileOutputStream(filename.substring(0, filename.length()-4) + "Output.txt"));
- }
- catch(FileNotFoundException e)
- {
- JOptionPane.showMessageDialog(null,"The file could not be created.");
- }
- while(inFile.hasNextLine()) //is supposed to run while the .txt file has lines available
- {
- String line = inFile.nextLine();
- if(line.equals(""))
- {
- outFile.println("");
- System.out.println("There is nothing in this line of text. Moving on!");
- break;
- }
- for (String lineSplit: line.split(" "))
- {
- System.out.print(pig(lineSplit) + " ");
- outFile.print(pig(lineSplit) + " ");
- }
- System.out.println("Moving on!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement