问题
Here I am trying to read a document that has words and corresponding abbreviations. E.g.
one,1
two,2
easy,ez
Each line is separated, and the words are separated from the abbreviations with a comma.
When I want the buffered reader to keep reading lines using
while ((line = br.readLine()) != null)
it won't. It will only read the first line. This means it will display the abbreviation for one, which is 1. But if I input another value say two, I get this error message.
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:
here's the code
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;
}
回答1:
You have made the conditional expression in for
loop as i lesser than or equal to line.length(). That's wrong.i
shouldn't be equal to the length of the line. Change this
for(int i = 0; i <= line.length(); i++)
to
for(int i = 0; i < line.length(); i++)
you can't make i
equal to the length of line. The index starts from 0
. so, the valid indexes would be from 0
to length-1
回答2:
This is incorrect:
for(int i = 0; i <= line.length(); i++) {
as it is accessing one too many. To correct:
for(int i = 0; i < line.length(); i++) {
Note that the posted code does not close()
the reader if the abbreviation is found. Use a finally block or try-with-resources statement to ensue the reader is closed.
Other:
- String.split() could be used to separate the line into two tokens instead of explicitly coding the separation.
A Properties style file could be used to store the abbreviation-to-term map instead of a comma separated file. The
Properties
class provides a mechanism for loading the file and methods for searching the map:// If the 'abbreviations.txt' file is not updated // by the program then arrange for it to be loaded once. // try (final FileInputStream is = new FileInputStream("abbreviations.txt")) { final Properties p = new Properties(); p.load(is); return p.getProperty(input); }
回答3:
Check out the for
loop part of your code:
for(int i = 0; i <= line.length(); i++) {
It should be changed to this:
for(int i = 0; i < line.length(); i++) {
Notice how <=
was changed to <
:
for(int i = 0; i <= line.length(); i++) {
//vs:
for(int i = 0; i < line.length(); i++) {
For pretty much all coding, including java
, counting starts at 0. So where you would normally say 5, in coding you would say 4.
Let's say the length of the array is 3
, meaning it contains 0
, 1
, and 2
. So, when doing this:
for(int i = 0; i <= line.length(); i++) {
//length of the line is 3, so:
for(int i = 0; i <= 3; i++) {
now, it will loop threw the numbers 0
, 1
, 2
, and 3
. Yet, the line is only contains only 0
, 1
, and 2
. So, if we were to do this:
for(int i = 0; i < line.length(); i++) {
//length of the line is 3, so:
for(int i = 0; i < 3; i++) {
Using this new code, it will loop threw 0
, 1
, and 2
, the same length of our line, 0
, 1
, and 2
.
来源:https://stackoverflow.com/questions/22840689/can-someone-help-me-to-get-buffered-reader-to-work-in-java