【发布时间】:2014-01-28 20:10:02
【问题描述】:
我目前正在创建一个程序,该程序从用户那里获取后缀(“ing”、“er”等),然后将该后缀附加到输入文件中的单词列表中。问题是我正在使用缓冲读取器来获取用户想要使用的后缀,但我不断收到错误(线程“main”java.io.IOException 中的异常:流已关闭) 当我编译时。您会在下面注意到,我不会在任何地方关闭缓冲阅读器,因为那是当我收到错误时(我应该在哪里关闭它?)我只是想获取用户的输入并将其设置为要在其中使用的字符串变量班上。有什么建议吗??
public static String GetSuffix (String psSingular1) throws IOException {
//Use BufferedReader to get suffix
BufferedReader brGetSuffix = new BufferedReader(new InputStreamReader(System.in));
//Prompt the user for a suffix
System.out.println("Please insert a suffix that you would like add to the words (ex. er, ing, ance,etc.)");
//Set a string to the user entered suffix
String sUserSuffix = brGetSuffix.readLine();
//Initialize sSuffix
String sSuffix = "";
//Each string represents the last characters of the word from the input file
String sLast1 = psSingular1.toLowerCase().substring(psSingular1.length()-1, psSingular1.length());
String s2ndLast = psSingular1.toLowerCase().substring(psSingular1.length()-2, psSingular1.length()-1);
//This string represents the last letter of the word
String sLastLetter = psSingular1.toLowerCase().substring(psSingular1.length()-1, psSingular1.length());
//This string represents a double vowel. It will be used in a word like "keep"
String sDoubleVowel = psSingular1.toLowerCase().substring(psSingular1.length()-3, psSingular1.length()-1);
//This String will represent the first character of the users suffix which will be needed to determine how the suffix is added to the word
String s1stSuffix = sUserSuffix.toLowerCase().substring(0, 0);
//If the word ends in 2 vowels and a consonant the suffix is added
if (bIsVowel(sDoubleVowel) && !bIsVowel(sLast1)){
sSuffix = psSingular1 + sUserSuffix;
}
//Checks if psSingular1 ends in a consonant and y, changes the y to i and adds a suffix.
else if (sLast1.equals("y") && !bIsVowel(s2ndLast)){
//Drop the "y" add "ies"
sSuffix = psSingular1.substring(0, psSingular1.length()-1) + "i" + sUserSuffix;
}
//Checks if psSingular1 ends in a vowel and y
else if (sLast1.equals("y") && bIsVowel(s2ndLast)){
sSuffix = psSingular1 + sUserSuffix;
}
//Checks if psSingular1 ends in e and if the suffix starts with a vowel
else if (sLast1.equals("e") && bIsVowel(s1stSuffix)){
sSuffix = psSingular1.substring(0, psSingular1.length()) + sUserSuffix;
}
//Checks if psSingular1 ends in e and if the suffix starts with a consonant
else if (sLast1.equals("e") && !bIsVowel(s1stSuffix)){
sSuffix = psSingular1 + sUserSuffix;
}
//Checks if the word ends in two consonants
else if (!bIsVowel(s2ndLast) && !bIsVowel(sLast1)){
sSuffix = psSingular1 + sUserSuffix;
}
//If the the word ends in a vowel and a consonant the last letter is doubled and the suffix is added
else if (bIsVowel(s1stSuffix) && bIsVowel(s2ndLast) && !bIsVowel(sLast1)){
sSuffix = psSingular1 + sLastLetter + sUserSuffix;
}
//Add the suffix
else {
sSuffix = psSingular1 + sUserSuffix;
}
return sSuffix;
}
【问题讨论】:
-
您可以尝试使用:
BufferedReader brGetSuffix = new BufferedReader(new InputStreamReader(new CloseShieldInputStream(System.in)));并告诉我们它是否仍然不起作用? -
@ccjmne 复制并粘贴您所拥有的内容,但在 CloseShieldInputStream 下出现错误。它是可能需要导入的类的一部分吗?
-
糟糕...抱歉,您是对的...来自
org.apache.commons.io.input。实际上,也许你可以使用Scanner scanner = new Scanner(System.in);然后String sUserSuffix = scanner.nextLine();。如果您只是从用户输入中获取一些字符,则可能不需要使用 BufferedReader。我知道它没有回答你的问题,但也许至少你可以继续:) -
@ccjmne 我决定使用扫描仪并且我能够编译,但我现在收到错误:(线程“main”java.util.NoSuchElementException 中的异常:未找到行)。我确实输入了一些东西,所以我不明白为什么“找不到行”。我也关闭了扫描仪,有什么想法吗?
-
怎么样:
if scanner.hasNextLine() { sUserSuffix = scanner.nextLine() }它必须在某一时刻认真工作......
标签: java error-handling bufferedreader ioexception