我在构建参数传递时遇到了麻烦。我试过设置变量String pluralizedWord = makePlural(word)等。但这没有帮助。我已经得到一个找不到变量复数和wordWithSuffix的符号错误。你能看一下吗?非常感激。如果我还没有提到过,那就是Java。在我的代码顶部,您可以看到注释变量。我最初在全局发布了这些变量,但为了使我的代码更好,我需要只有局部变量并返回需要通过参数传递传递的变量。
import java.lang.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
public class newVowel {
public static FileInputStream fileInStream; //call the FileInputStream and name as fileInStream
public static InputStreamReader fileInReader; //call the InputStreamReader and name as fileInReader
public static BufferedReader reader; //call the BufferedReader and simply name it as such, reader
//private static String line, word, suffix, plural, wordWithSuffix; //call String variables
//private static int length, lengthOfWord, spaceIndex, counter; //call integer variables to be used later
//private static char firstLetter, secondLetter, lastLetter, c; //call the character variables
public static void initiateFile() throws IOException //the initiateFile method reads the external text file and sets all contents of file into the variable reader
{
fileInStream = new FileInputStream("C:\\!!VHSAPCSData\\vowels.txt"); //location of file
fileInReader = new InputStreamReader(fileInStream); //inputs data
reader = new BufferedReader(fileInReader); //reads data
}
public static String makePlural(String word) //the makePlural method, simply makes the words plural
{
String plural;
int length, lengthOfWord;
char lastLetter, secondLetter;
lengthOfWord = word.length(); //calculates the length of the word
lastLetter = word.charAt(lengthOfWord-1); //calculates the last letter
secondLetter = word.charAt(lengthOfWord-2); //calculates the second to last letter
if(((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L')) && ((secondLetter != 'A')||(secondLetter != 'C')||(secondLetter != 'S')||(secondLetter != 'L')))
{ //this if statement checks for the specified "vowels" A, C, S, and L in specific combinations. If the word ends in only one vowel,
plural = word.substring(0, (lengthOfWord-1));//the original word drops the last letter (vowel) as the directions state, and then
plural += "G"; //"G" is added to the end of the word
}
if(((lastLetter != 'A')||(lastLetter != 'C')||(lastLetter != 'S')||(lastLetter != 'L')) && ((secondLetter == 'A')||(secondLetter == 'C')||(secondLetter == 'S')||(secondLetter == 'L')))
plural = word + "GH"; //this if statement checks for the specified "vowels" A, C, S, and L in specific combinations. If, of the last two letters, the second to last letter
//is a vowel, "GH" is added to the end of the word
if(((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L')) && ((secondLetter == 'A')||(secondLetter == 'C')||(secondLetter == 'S')||(secondLetter == 'L')))
{
String lastChar = Character.toString(lastLetter); //this if statement checks for the specified "vowels" A, C, S, and L in specific combinations. If the word ends in two vowels,
plural = word + lastChar + "H"; //then the last letter is doubled and "H" is added to end of word
}
String pluralizedWord = plural;
return pluralizedWord;
}
public static String addOnSuffix(String word, String suffix) //this is the addOnSuffix method, hence where the suffix is added onto the word
{
String wordWithSuffix;
int length, lengthOfWord, counter;
char firstLetter, secondLetter, lastLetter, c;
lengthOfWord = word.length(); //calculates length of word
lastLetter = word.charAt(lengthOfWord-1); //calculates last letter of word
secondLetter = word.charAt(lengthOfWord-2); //calculates second to last letter of word
firstLetter = suffix.charAt(0); //calculates first letter of word
if(((firstLetter == 'A')||(firstLetter == 'C')||(firstLetter == 'S')||(firstLetter == 'L'))) //if statement which applies if the first letter of word is a "vowel"
{
if(((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L')) && ((secondLetter != 'A')&&(secondLetter != 'C')&&(secondLetter != 'S')&&(secondLetter != 'L')))
{ //if statement which applies if the last letter of word is a vowel, yet second to last letter is a "non-vowel"
String append = suffix.substring(1); //suffix is appended by dropping first letter of suffix then adding the suffix
wordWithSuffix = word + append;
}
if(((lastLetter != 'A')&&(lastLetter != 'C')&&(lastLetter != 'S')&&(lastLetter != 'L')) && ((secondLetter == 'A')||(secondLetter == 'C')||(secondLetter == 'S')||(secondLetter == 'L')))
wordWithSuffix = word + suffix; //if statement which applies if the second to last letter of word is a vowel, yet last letter is a "non-vowel"
//suffix is appended by merely adding it, no change to suffix
if(((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L')) && ((secondLetter == 'A')&&(secondLetter == 'C')&&(secondLetter == 'S')&&(secondLetter == 'L')))
{ //if statement which applies if both the second to last and last letters are "vowels," if so
String firstChar = Character.toString(firstLetter); //then the first letter will be added to word, and then the suffix (unchanged)
wordWithSuffix = word + firstChar + suffix;
}
}
else //complicated else statement that puts together plurals and suffixes of words that don't begin with a "vowel"
{
if(((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L')) && ((secondLetter != ('A'))&&(secondLetter != 'C')&&(secondLetter != 'S')&&(secondLetter != 'L')))
{ //if the word does not begin with a "vowel" and the last letter does, yet the second to last does not, then
String firstChar = Character.toString(firstLetter); //the first character is parsed to a string firstChar,
wordWithSuffix = word + firstChar + suffix; //and the word is put together as the word + the first character + add the suffix
}
if(((lastLetter != ('A'))&&(lastLetter != 'C')&&(lastLetter != 'S')&&(lastLetter != 'L')) && ((secondLetter == 'A')||(secondLetter == 'C')||(secondLetter == 'S')||(secondLetter == 'L')))
wordWithSuffix = word + suffix; //if the word does not begin with a "vowel" and the second to last letter does, yet the last does not, then
else //easily enough, the word is put together as the word plus the suffix
{
if((lastLetter == 'A')||(lastLetter == 'C')||(lastLetter == 'S')||(lastLetter == 'L'))
{ //if the word does not begin with a "vowel", yet the last letter is a "vowel,"
counter = (lengthOfWord-1); //assign integer counter to count the length of the word minus 1,
c = word.charAt(counter); //then assign character variable c to the character at the counter value, then
while ((c == 'A')||(c == 'C')||(c == 'S')||(c == 'L')) //the while statement checks that character for whether or not it is a "vowel,"
{ //
counter--; //if so, then counter reassigns value to counter - 1, and then
c = word.charAt(counter); //reassigns character to the one at the new value
} //
String part1 = word.substring(0, counter+1); //String part1 is generated with substring of word between character 0 and the character at counter - 1
String part2 = word.substring((counter+2), lengthOfWord); //String part2 is generated with substring of word between the character at counter + 2 and the end of the word
String newWord = part1 + part2; //String newWord is created by putting together parts 1 and 2
wordWithSuffix = newWord + suffix; //The suffixed word is outputed as the newWord + the suffix
}
else
{
counter = (lengthOfWord-1); //if word doesn't fall under any previous conditional statements, then counter is assigned a value equal to the length of
c = word.charAt(counter); //the word - 1, and character c is equal to the character at the value counter
while ((c != 'A')||(c != 'C')||(c != 'S')||(c != 'L')) //the while statement checks that character for whether or not is is a "vowel,"
{ //
counter--; //if so, then counter reassigns value to counter - 1, and then
c = word.charAt(counter); //reassigns character to the one at the new value
} //
String part1 = word.substring(0, counter); //String part1 is generated with substring of word between character 0 and the character at the counter value
String part2 = word.substring((counter+1), lengthOfWord); //String part2 is generated with substring of word between the character at counter + 1 and the end of the word
String newWord = part1 + part2; //String newWord is created by putting together parts 1 and 2
wordWithSuffix = newWord + suffix; //The suffixed word is outputed as the newWord + the suffix
}
}
}//end of else
String suffixedWord = wordWithSuffix;
return suffixedWord;
}//end of method
public static void printFinalData(String line, String word, String suffix, String plural, String wordWithSuffix) //method which outputs the final data, aka, the plural of word, suffixed word, etc.
{
System.out.println("Line Input: " + line);
System.out.println("Word: " + word);
System.out.println("Suffix: " + suffix);
System.out.println("Plural Of Word: " + plural);
System.out.println("New Word With Suffix: " + wordWithSuffix);
System.out.println();
}
public static void main(String[] args) throws IOException //this main method is what tells the other methods when to inititate (which order)
{
System.out.println("**********************************");
System.out.println("* String Project *");
System.out.println("* AP Computer Science A *");
System.out.println("* Joshua Nasiatka *");
System.out.println("* Winter 2012 (Senior Yr) *");
System.out.println("**********************************");
System.out.println();
System.out.println("The following is the output:");
System.out.println();
//String word = "", suffix = "";
initiateFile();
parseFile(pluralizedWord, suffixedWord);
//plural = makePlural(word);
makePlural(word);
//wordWithSuffix = addOnSuffix(word, suffix);
addOnSuffix(word, suffix);
}
public static void parseFile(String pluralizedWord, String suffixedWord) throws IOException
{
String line, word, suffix;
int spaceIndex, length;
line = reader.readLine(); //reads the input of each line of the external .txt file entitled "vowels.txt"
while(line != null) //continues to input data until a null is reached in the reader
{
spaceIndex = line.indexOf(" ");
length = line.length();
word = line.substring(0, spaceIndex);
suffix = line.substring((spaceIndex+1), length);
makePlural(word);
addOnSuffix(word, suffix);
printFinalData(line, word, suffix, pluralizedWord, suffixedWord);
line = reader.readLine();
}
}
}//end class`
答案 0 :(得分:0)
你是在谈论主要的这一行//plural = makePlural(word);
吗?当然,编译器无法找到符号,我也不能。
在plural
中使用word
之前,您需要定义引用并根据需要创建wordWithSuffix
,main
,{{1}}等对象。
答案 1 :(得分:0)
也许只是这样做
String word = "aWord", "aSuffix";
String plural = makePlural(word);
String wordWithSuffix = addOnSuffix(word, suffix);
我可以看到你做了一件事但是注释掉了。