Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. //name: Liam Rathke date: 20 Sep
  2. import java.util.*;
  3. import java.io.*;
  4. import javax.swing.JOptionPane;
  5. public class PigLatin
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //part_1_using_pig();
  10. part_2_using_piglatenizeFile();
  11. }
  12.  
  13. public static void part_1_using_pig()
  14. {
  15. Scanner sc = new Scanner(System.in);
  16. while(true)
  17. {
  18. System.out.print("\nWhat word? ");
  19. String s = sc.next();
  20. if (s.equals("-1"))
  21. System.exit(0);
  22. String p = pig(s);
  23. System.out.println("***** " + p + " *****");
  24. }
  25. }
  26. public static String pig(String s)
  27. {
  28. boolean firstCharCapitalized = false; //sets the first character's capitalization to false
  29. if(!s.substring(0,1).equals(s.substring(0,1).toLowerCase())) //if the letter is not the same as the lower case letter
  30. {
  31. firstCharCapitalized = true; //it is capitalized
  32. //System.out.println("The first character is capitalized!");
  33. s = s.toLowerCase();
  34. //System.out.println("Lower case check: " + s);
  35. }
  36. String punctuationChars = ",.!?:"; //list of characters
  37. String punctuation = ""; //default punctuation at the end of a string
  38. for(int o = 0; o < punctuationChars.length(); o++) //runs through the punctuation for the last index of the word
  39. {
  40. if(punctuationChars.substring(o, o + 1).equals(s.substring(s.length() - 1, s.length()))) //last char equals one of the punctuations
  41. {
  42. punctuation = s.substring(s.length()-1, s.length()); //changes the punctuation string to the appropriate char
  43. s = s.substring(0, s.length()-1);
  44. //System.out.println("Punctuation detected: " + punctuation);
  45. }
  46. }
  47. String vowels = "aeiou"; //possible vowels
  48. if(s.substring(0,1).equals("y")) //for special y case
  49. {
  50. //System.out.println("Y case returned!");
  51. int yCount = 0;
  52. while(!vowels.contains(s.substring(yCount, yCount + 1)))
  53. yCount ++;
  54. //System.out.println("The yCount is equal to " + yCount);
  55. String yFinal = s.substring(yCount, s.length()) + s.substring(0, yCount) + "ay";
  56. return yFinal; //returns y case
  57. }
  58. for(int i = 0; i < vowels.length(); i++)
  59. {
  60. if(s.substring(0, 1).equals(vowels.substring(i, i+1)))
  61. {
  62. //System.out.println("There is a vowel at the start!");
  63. if(firstCharCapitalized == true)
  64. {
  65. s = s.substring(0,1).toUpperCase() + s.substring(1, s.length());
  66. //System.out.println("The first character in pig latin is now capitalized! Check: " + s);
  67. }
  68. return(s + "way" + punctuation);
  69. }
  70. }
  71. vowels = vowels + "y";
  72. String consonants = s;
  73. int numCons = 0;
  74. vowelTestLoop: for(int j = 0; j < consonants.length()-1; j++)
  75. {
  76. for(int p = 0; p < vowels.length(); p++)
  77.  
  78. {
  79. if(s.substring(j, j+1).equals(vowels.substring(p, p+1)))
  80. {
  81. numCons = j - 1;
  82. //System.out.println("There are " + (numCons) + " consonants before the first vowel!");
  83. //System.out.println("Vowel Check! " + s.substring(j, j+1) + ", " + vowels.substring(p, p+1));
  84. break vowelTestLoop;
  85. }
  86. }
  87. numCons = 0;
  88. }
  89. int newnumCons = numCons + 1;
  90. int vowelTester = 0;
  91. //System.out.print("Vowels in string:");
  92. for(int u = 0; u < vowels.length(); u++)
  93. {
  94. if(s.contains("" + vowels.charAt(u)))
  95. {
  96. //System.out.print(" " + vowels.charAt(u));
  97. vowelTester++;
  98. }
  99. }
  100. //System.out.println("");
  101. //System.out.println("There are " + vowelTester + " total vowels!");
  102. if(vowelTester == 0)
  103. {
  104. return ("INVALID");
  105. }
  106. if((vowelTester == 1) && (vowels.contains("" + s.charAt(s.length()-1))))
  107. {
  108. return("" + s.charAt(s.length()-1) + s.substring(0, s.length() - 1) + "ay");
  109. }
  110. String firstCons = consonants.substring(0, newnumCons); //+ 1);
  111. String otherChars = consonants.substring(newnumCons, consonants.length());
  112. //System.out.println("The firstCons String is " + firstCons);
  113. //System.out.println("The otherChars String is " + otherChars);
  114. if(((firstCons.substring(firstCons.length() - 1, firstCons.length()).equals("q"))) && ((otherChars.substring(0, 1)).equals("u")))
  115. {
  116. //System.out.println("There is a q before the u!");
  117. String qAtStart = otherChars.substring(1, otherChars.length()) + firstCons + "uay";
  118. if(firstCharCapitalized == true)
  119. {
  120. qAtStart = qAtStart.substring(0,1).toUpperCase() + qAtStart.substring(1, qAtStart.length());
  121. //System.out.println("The first character in pig latin is now capitalized! Check: " + qAtStart);
  122. }
  123. return qAtStart;
  124. }
  125. if(firstCharCapitalized == true)
  126. {
  127. otherChars = otherChars.substring(0,1).toUpperCase() + otherChars.substring(1, otherChars.length());
  128. //System.out.println("The first character in pig latin is now capitalized! Check: " + s);
  129. }
  130. //System.out.println("Normal case detected!");
  131. return (otherChars + firstCons + "ay" + punctuation);
  132. }
  133.  
  134. public static void part_2_using_piglatenizeFile() throws IOException
  135. {
  136. Scanner sc = new Scanner(System.in);
  137. System.out.print("What filename? ");
  138. String filename = sc.next();
  139. Scanner inFile = new Scanner(new File(filename)); //PigLatin.txt
  140. piglatenizeFile( inFile, filename );
  141. System.out.println("Piglatin done!");
  142. sc.close();
  143. }
  144. /******************************
  145. * take in a filename, and creates a file that is the inputted file
  146. * fully piglatenized. The outputFile should be in piglatin form
  147. * PigLatin.txt should become IgLatinpay.txt.
  148. *
  149. * Note: filename will have .txt on it.
  150. ******************************/
  151. public static void piglatenizeFile(Scanner inFile, String filename) throws IOException
  152. {
  153. try{
  154. inFile = new Scanner(new File(filename));
  155. }
  156. catch(FileNotFoundException e)
  157. {
  158. JOptionPane.showMessageDialog(null,"The file could not be found.");
  159. System.exit(0);
  160. }
  161. PrintStream outFile = null;
  162. try{
  163. outFile = new PrintStream(new FileOutputStream(filename.substring(0, filename.length()-4) + "Output.txt"));
  164. }
  165. catch(FileNotFoundException e)
  166. {
  167. JOptionPane.showMessageDialog(null,"The file could not be created.");
  168. }
  169. while(inFile.hasNextLine()) //is supposed to run while the .txt file has lines available
  170. {
  171. String line = inFile.nextLine();
  172. if(line.equals(""))
  173. {
  174. outFile.println("");
  175. System.out.println("There is nothing in this line of text. Moving on!");
  176. break;
  177. }
  178. for (String lineSplit: line.split(" "))
  179. {
  180. System.out.print(pig(lineSplit) + " ");
  181. outFile.print(pig(lineSplit) + " ");
  182. }
  183. System.out.println("Moving on!");
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement