Array Only Prints Certain Elements and Not Others



I am currently in a Intro to Computer Language class and everyone few lessons I have to develop some minimal programs (we are given 5 different prompts and have to pick three of them to complete). Due to time, I moved on and completed a different program, but I still want to understand what is going wrong with this one. It is supposed to translate a given phrase into Pig Latin using for loops and different methods (as broken down in their template, which I cannot change, though I know there is a more efficient way). I can get the words in the phrases to translate, but when I print out the array (either by converting it to a string or running a for loop to print each element out separately) some of the elements only print the reference code. Could someone tell me what’s going on? Below is the code and then a sample of a few print outs.

import java.util.Arrays;
import java.util.Scanner;

public class PigLatin {
  public static void main (String[] args){
      Scanner scnr = new Scanner(System.in);
      String userWord;

      userWord = scnr.nextLine();
      userWord = userWord.toLowerCase();

      String[] wordArry = userWord.split(" ");

      print(wordArry);
  }

  public static String[] translate(String[] words){
     String[] pigLatin = new String[words.length];

     for (int i = 0; i < words.length; ++i) {
         if (isVowel(words[i].charAt(0)) == true){
             pigLatin[i] = words + "ay"; 
         }
         else if (words[i].charAt(0) == 'y') {
             pigLatin[i] = words[i].substring(findFirstVowel(words[i]), words[i].length()) + words[i].substring(0, findFirstVowel(words[i])) + "ay";
         }
         else {
             pigLatin[i] = words[i].substring(findFirstVowel(words[i]), words[i].length()) + words[i].substring(0, findFirstVowel(words[i])) + "ay";
         }
     }
     return pigLatin;
  }
  public static int findFirstVowel(String s){
      char[] vowList = {'a','e','i','o','u','y'};

        for (int i = 1; i < s.length(); ++i) {
            for (int j = 0; j < vowList.length; ++j) {
                if (s.charAt(i) == vowList[j]) {
                    return i;
                }
            }
        }
    return -1;
   }
  public static boolean isVowel(char c){
      boolean vowel = false;
      char[] vowList = {'a','e','i','o','u'};

      for (int i = 0; i < vowList.length; ++i) {
          if (c == vowList[i]) {
              vowel = true;
          }
      }
      return vowel;
  }
  public static void print(String[] words){

      String[] newArry = new String[words.length];

      for (int i = 0; i < words.length; ++i) {
          newArry[i] = words[i];
      }

      String finalPrint = Arrays.toString(translate(newArry));
      finalPrint = finalPrint.replace("[", "");
      finalPrint = finalPrint.replace(",", "");
      finalPrint = finalPrint.replace("]", "");

      System.out.println(finalPrint);

  }
}

Here are some of the printed responses:

Input: the rain in spain stays mainly in the plain Output: ethay ainray [Ljava.lang.String;@17c68925ay ainspay aysstay ainlymay [Ljava.lang.String;@17c68925ay ethay ainplay Expected: ethay ainray inay ainspay aysstay ainlymay inay ethay ainplay

Input: you should have stayed with the soup question OutPut: ouyay ouldshay avehay ayedstay ithway ethay oupsay uestionqay This print’s out correctly

Input: the stuff that dreams are made of Output: ethay uffstay atthay eamsdray [Ljava.lang.String;@17c68925ay ademay [Ljava.lang.String;@17c68925ay Expected: ethay uffstay atthay eamsdray areay ademay ofay

I cannot find an answer as to why this is happening. Please let me know if you need any additional information. Thanks!

Answer

pigLatin[i] = words + "ay";

This line. You are appending string to an array. Change it to:

pigLatin[i] = words[i] + "ay";

Side note 1:

for (int i = 0; i < words.length; ++i) {
    newArry[i] = words[i];
}

This loop can be changed to:

System.arraycopy(words, 0, newArry, 0, words.length);

Side note 2:

Splitting on "\s+" is better. It also takes care of multiple spaces.


Side note 3:

if (isVowel(words[i].charAt(0)) == true) {

This can be simplified as:

if (isVowel(words[i].charAt(0))) {

Side note 4:

for (char value : vowList) {
    if (c == value) {
        vowel = true;
        break;
    }
}

Using break for slightly better performance. Also, using foreach loop.


Final side note:

finalPrint = finalPrint.replace("[", "")
        .replace(",", "")
        .replace("]", "");

Chaining of replace calls.



Source: stackoverflow