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

时间:2014-01-10 01:02:55

标签: java error-handling bufferedreader ioexception

我目前正在创建一个程序,该程序从用户获取后缀(“ing”,“er”等),然后将该后缀附加到输入文件中的单词列表。问题是我使用缓冲读取器来获取用户想要使用的后缀,但是我一直收到错误(线程中的异常“main”java.io.IOException:Stream closed) 当我编译。您将在下面注意到我没有在任何地方关闭缓冲读卡器,因为那是我收到错误的时候(我应该在哪里关闭它?)我只想取用户的输入并将其设置为字符串变量以便在其中使用班级。有什么建议??

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;

}

1 个答案:

答案 0 :(得分:0)

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