わかりましたので、以下のステートメントを return ステートメントに変換してみましたが、 「このステートメントは文字列型の変数を返す必要があります」と返されfinalString
ても、常に通知され続けます。個々の if ステートメント、for ステートメント、その外側にfinalString
入れようとしましたが、うまくいきません。return finalString
助けや提案をいただければ幸いです。[コードの更新] まだ機能しません。finalString 値は if ステートメントによって変更されません。これはまさに私がやりたいことです。多分、finalString の値が if 文を通らないと思いますか?
[コード]
import java.util.Scanner;
public class pLat//pig latin program
{
/**
* Method to test whether a character is a letter or not.
* @param c The character to test
* @return True if it's a letter
*/
private static boolean isLetter(char c) {
return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
}
///////////////////////////////////////////
private static String output(String input)//processes the word using basic rules including the q and u rule
{
//the string that will hold the value of the word entered by the user
char s;//the first character of the string
char m;
int l = input.length();//determines the length of the string
String endString;
String startString;
String finalString = ""; //the final output
String mtr;
String lowercase;//the entered string all converted to lowercase
for(int k =0;k<l;k++)//checks all letters in order to see which is a vowel
{
s = input.charAt(k);
if(s == 'q'|| s=='Q' && input.charAt(k+1)=='u')//if the first vowel is a "u" and the letter before it is a "q"
{
endString = input.substring(0,k+2);//makes the endString also include u
endString = endString +"ay";
startString = input.substring(k+2,l);
finalString = startString + endString;
//System.out.println(finalString);
return finalString;
}
if(s=='a'||s=='e'||s=='i'||s=='o'||s=='u'||s=='A'||s=='E'||s=='I'||s=='O'||s=='U'||s=='y'||s=='Y')//if its a vowel or "y" than executes commands below
{
endString = input.substring(0, k);//gets the letters before the vowel
endString = endString + "ay";
startString = input.substring(k,l);//gets the letters after the vowel
finalString = startString + endString;
//System.out.println(finalString);//prints the final result which is the combination of startString with endString
//stops code after doing the above
return finalString;
}
else if(k==l-1)//if its the end of the word
{
finalString = "ERROR";
return finalString;
}
}
System.out.println(finalString);
return finalString;
}///////////////////////////////////
// public static void process(String input)//will take care of the punctuation
// {
// String latin = "";
// int i = 0;
// while (i<input.length()) {
//
// // Takes care of punctuation and spaces
// while (i<input.length() && !isLetter(input.charAt(i))) {
// latin = latin + input.charAt(i);
// i++;
// }
// latin = latin + output(input);
// System.out.println(latin);
// }
//
// }
public static void main(String[] args)
{
String str;//this will be the input string by the user
Scanner scanner = new Scanner(System.in);//this scanner will register the input value
System.out.println("Enter a Word: ");
str = scanner.next();//stores the input string
output(str);//outputs it using basic gramatical rules
}
}