我有一個簡單的程序讀取文件。現在線路之間有一個空白區域。我得到StringIndexOutOfBoundsException:String索引超出範圍:0錯誤。請幫助StringIndexOutOfBoundsException:字符串索引超出範圍:0讀取文件時
class main{
public static void main(String args[]) {
String str;
try {
BufferedReader br = new BufferedReader (new FileReader("train.txt"));
while((str=br.readLine())!=null){
System.out.println(str);
int a=str.charAt(0);
if(str.trim().length()==0){
System.out.println("stupid");
}
else if(a==32){
System.out.println("ddddd");
}
else if(str.charAt(0)=='A' ||str.charAt(0)=='a'){
System.out.println("hahha");
}
else if(str.charAt(0)=='C' ||str.charAt(0)=='c'){
System.out.println("lol");
}
else if(str.charAt(0)=='D' ||str.charAt(0)=='d'){
System.out.println("rofl");
}
else{
System.out.println("blank");
}
}
}
catch (FileNotFoundException e){
System.out.println(e);
}
catch (IOException e){
System.out.println(e);
}
}
到達2014年3月21日10:30 38472特快 取消40003 這是我的文件中包含的數據。我的代碼無法通過到達和取消行之間的空格 – Sidarth
使用堆棧跟蹤發佈您的異常。 –