I'm having a hard time with my code in BlueJ. I keep getting this error, all syntax and other issues are solved, but it keeps on providing this error. It says it's in line 22:
StringIndexOutOfBoundsException: String index out of range: -1
Here's the code I've written:
public static void main() {
String songInfo = MediaFile.readString();
int index = songInfo.indexOf("|");
System.out.println("My Favorites");
System.out.println("------------");
while(index > 0){
String title = songInfo.substring(0, index);
songInfo = songInfo.substring(index + 1);
index = songInfo.indexOf("|");
String ratingStr = songInfo.substring(0, index); //this is the line where the error occurs
int rating = Integer.valueOf(ratingStr);
if(rating >= 8){
System.out.println(title + "(" + rating + ")");
}
songInfo = songInfo.substring(index + 1);
index = songInfo.indexOf("|");
}
}
How can I fix this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…