在這裏,我試圖閱讀一個文字,有文字和相應的縮寫。例如。有人可以幫助我使緩衝閱讀器在Java中工作嗎?
one,1
two,2
easy,ez
每一行都是分開的,單詞與縮寫之間用逗號隔開。
當我想緩衝的讀者繼續閱讀使用行
while ((line = br.readLine()) != null)
不會。它只會讀第一行。這意味着它將顯示一個的縮寫,即1。但是,如果我輸入另一個值,例如兩個,我會收到此錯誤消息。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at shortener.NewClass.Short(NewClass.java:41)
at shortener.NewClass.main(NewClass.java:26)
Java Result:
這裏的代碼
public static String Short(String input) {
try {
FileReader fr = new FileReader("abbreviations.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
for(int i = 0; i <= line.length(); i++) {
if(line.charAt(i) == ','){
String strOrig = line.substring(0, i);
if(strOrig.equals(input)) {
String strAbv = line.substring(line.length()-1);
return strAbv;
}
}
}
}
br.close();
} catch (IOException e) {
out.println("File Not found");
}
return null;
}
感謝它現在的作品:) –
很高興,它的工作:) – Keerthivasan