HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Pig Latin in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javalatinpig

Problem

I've done my second application in Java now. I want to know if:

  • This code is efficient



  • This code can be written in a shorter and faster way



  • Any flaws



  • Any misuse or better use of concepts



Main Class:

package Excercises;

public class Main {
    public static void main(String[] args) {
        PigLatin pigLatin = new PigLatin();

        pigLatin.initializeText();
        pigLatin.getWord();
        pigLatin.convertWordToStringArray();
        pigLatin.convertArrayToList();
        pigLatin.moveFirstLetterToLast();
        pigLatin.mergeList();

        System.out.println(pigLatin.returnNewWord());
    }
}


PigLatin class:

package Excercises;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PigLatin {
    private String word;
    private String newWord;
    private String[] Array;
    private List list = new ArrayList();

    public void initializeText() {
        System.out.print("Welcome to the Pig Latin Game! Please enter a word: ");
    }

    public void getWord() {
        Scanner scan = new Scanner(System.in);
        this.word = scan.next();
    }

    public void convertWordToStringArray() {
        this.Array = word.split("");
    }

    public void convertArrayToList() {
        for(String s : this.Array) {
            this.list.add(s);
        }
    }

    public void moveFirstLetterToLast() {
        this.list.add("-");
        this.list.add(this.Array[0]);
        this.list.remove(0);
        this.list.add("ay");
    }

    public void mergeList() {
        for(int i = 0; i < list.size(); i++) {
            this.newWord = this.newWord + list.get(i);
        }
        this.newWord = this.newWord.replaceAll("null", "");
    }

    public String returnNewWord() {
        return this.newWord;
    }
}

Solution

Input and general structure

Right now, the only possible way to use your class is via the command line. This makes testing really hard. I would change your getWord function to a simple setWord(String word) setter, and read the word in outside the class (somewhere in Main).

And in general, your PigLatin class should only transform words to piglatin, not also print and read. I would move initializeText to Main as well.

And your public interface for PigLatin is quite hard to manage. I would make all those methods private, and then either create a toPigLatin method, or just call them in returnNewWord.

I also think that you could merge some of the methods. At least convertWordToStringArray and convertArrayToList could well be in one method (wordToCharList or something).

Your naming is sometimes not very precise. For example, moveFirstLetterToLast also adds ay, which is unexpected.


This code is efficient

Probably not. You are iterating over the whole string twice, which is not necessary.


This code can be written in a shorter and faster way

Sure, there are lots of ways. For example:

String s;
   char firstChar = s.charAt(0);
   String piglatin = s.substring(1) + "-" + firstChar + "ay";


It's probably not the fastest way, but it should be faster than your approach.

Code Snippets

String s;
   char firstChar = s.charAt(0);
   String piglatin = s.substring(1) + "-" + firstChar + "ay";

Context

StackExchange Code Review Q#65349, answer score: 11

Revisions (0)

No revisions yet.