gpt4 book ai didi

Java/Eclipse - 在主方法之外使用 BufferedReader 时解决 IOException

转载 作者:太空宇宙 更新时间:2023-11-04 15:14:09 27 4
gpt4 key购买 nike

我目前正在创建一个程序,该程序从用户处获取后缀(“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;

}

最佳答案

似乎我们缺少一些代码(psSingular1 来自哪里?),但也许尝试在某处关闭缓冲读取器,看看是否有帮助。完成向阅读器添加输入后,您可以将其关闭。它甚至可能就在 return 语句之前。

关于Java/Eclipse - 在主方法之外使用 BufferedReader 时解决 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com