英文:
Array Only Prints Certain Elements and Not Others
问题
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[i] + "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);
}
}
输入: the rain in spain stays mainly in the plain
输出: ethay ainray inay ainspay aysstay ainlymay inay ethay ainplay
输入: you should have stayed with the soup question
输出: ouyay ouldshay avehay ayedstay ithway ethay oupsay uestionqay
输入: the stuff that dreams are made of
输出: ethay uffstay atthay eamsdray areay ademay ofay
英文:
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!
答案1
得分: 1
你在这个代码块中有一个错误(可能是拼写错误):
if (isVowel(words[i].charAt(0)) == true){
pigLatin[i] = words + "ay";
}
应该改为(注意不需要将布尔值与 true
进行比较):
if (isVowel(words[i].charAt(0))) {
pigLatin[i] = words[i] + "ay";
}
另外,在 print()
方法中不需要复制数组 - translate()
方法不会以任何方式修改输入数组。
最后,不必替换 Arrays.toString
的输出,可以使用 String.join
,这样你的打印方法会变成:
public static void print(String[] words){
System.out.println(String.join(" ", translate(words)));
}
英文:
You have a mistake (typo, likely) in this block:
if (isVowel(words[i].charAt(0)) == true){
pigLatin[i] = words + "ay";
}
It should be instead (note you don't need to compare boolean value with true
):
if (isVowel(words[i].charAt(0))) {
pigLatin[i] = words[i] + "ay";
}
Also, you don't need to copy your array in print() method - translate() doesn't modify input array in any way.
Finally, instead of replacing the output of Arrays.toString, you could use String.join, so your print method would look like:
public static void print(String[] words){
System.out.println(String.join(" ", translate(words)));
}
答案2
得分: 0
pigLatin[i] = words[i] + "ay";
----------
Side note 1:
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))) {
----------
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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论