当我编译和运行我的测试时,会出现以下消息:
StringIndexOutOfBoundsException: String index out of range: -3
我想我对子字符串做错了,但我不知道在哪里。
这应该被测试:
Argument for parsePathname: .mp3
Output getAuthor: empty String
Output getTitle: empty String
这是我的代码:
public void parseFilename(String filename)
{
//Dateiendung entfernen
int ending;
ending = filename.lastIndexOf('.');
filename = filename.substring(0,ending);
filetype = filename.substring(filename.length()-3);
//Abfrage, ob Bindestrich(hyphen) vorhanden
//i+1 ist Position vom Bindestrich
boolean has_hyphen = false;
int i;
for(i=0; i<filename.length(); i++)
{
if(filename.charAt(i) == ' ' && filename.charAt(i+1) == '-' && filename.charAt(i+2) == ' ')
{
has_hyphen = true;
break;
}
}
if (!has_hyphen || (filename.length() == 1 && filename.charAt(0) == '-'))
{
author ="";
title = filename;
}
if (filename.length() == 0 || (filename.charAt(0) == ' ' && filename.charAt(1) == '-' && filename.charAt(2) == ' '))
{
author = "";
title = "";
}
if (has_hyphen)
{
author = filename.substring(0,i);
author = author.trim();
title = filename.substring(i+2);
title = title.trim();
}
}