怎么会出现这个错误啊ArrayIndexOutOfBoundsException 有大佬吗
java吧
全部回复
仅看楼主
level 7
public class DemoTest {
public static void main(String[] args) throws IOException {
HashMap<String,String> hm = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("E:\\1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\2.txt"));
String line;
while((line=br.readLine())!=null){
String[] arr = line.split("\\.");
//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 这里是出错的地方
hm.put(arr[0],arr[1]);
}
for(String key:hm.keySet()){
String value = hm.get(key);
bw.write(key+"."+value);
}
bw.close();
br.close();
}
}
2020年12月07日 02点12分 1
level 7
hm.put(arr[0],arr[1]);
//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 这里是出错的地方 这个数组怎么越界了想了半天
2020年12月07日 02点12分 2
level 8
因为你的line用“\.”分割后的长度只有1,换句话说就是你的line中不包含“\.”,明白了吗
2020年12月07日 03点12分 3
w我的txt是(1.文字 2.文字...) 我用split分割的是 . 复制粘贴的时候用输入法的时候就变成\\。了
2020年12月07日 04点12分
回复改变就是好事♂:line.split("\\.")
2020年12月07日 05点12分
F分割后 不应该是 1和.后面的文字吗
2020年12月07日 05点12分
1