fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. PigLatin pigLatin = new PigLatin();
  8.  
  9. pigLatin.initializeText();
  10. pigLatin.getWord();
  11. pigLatin.convertWordToStringArray();
  12. pigLatin.convertArrayToList();
  13. pigLatin.moveFirstLetterToLast();
  14. pigLatin.mergeList();
  15.  
  16. System.out.println(pigLatin.returnNewWord());
  17. }
  18. }
  19.  
  20. class PigLatin {
  21. private String word;
  22. private String newWord;
  23. private String[] Array;
  24. private List<String> list = new ArrayList<String>();
  25.  
  26. public void initializeText() {
  27. System.out.print("Welcome to the Pig Latin Game! Please enter a word: ");
  28. }
  29.  
  30. public void getWord() {
  31. Scanner scan = new Scanner(System.in);
  32. this.word = scan.next();
  33. }
  34.  
  35. public void convertWordToStringArray() {
  36. this.Array = word.split("");
  37. }
  38.  
  39. public void convertArrayToList() {
  40. for(String s : this.Array) {
  41. this.list.add(s);
  42. }
  43. }
  44.  
  45. public void moveFirstLetterToLast() {
  46. this.list.add("-");
  47. this.list.add(this.Array[0]);
  48. this.list.remove(0);
  49. this.list.add("ay");
  50. }
  51.  
  52. public void mergeList() {
  53. for(int i = 0; i < list.size(); i++) {
  54. this.newWord = this.newWord + list.get(i);
  55. }
  56. this.newWord = this.newWord.replaceAll("null", "");
  57. }
  58.  
  59. public String returnNewWord() {
  60. return this.newWord;
  61. }
  62. }
Success #stdin #stdout 0.1s 380608KB
stdin
generic
stdout
Welcome to the Pig Latin Game! Please enter a word: generic-ay