列表中的空元素导致线程中出现异常;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围
我正在处理一个CSV文件,看起来像这样 正如您在冒号“date_Sald”中看到的,存在缺口 我正在尝试将日期缩短为月份(使用此方法,整个日期的前两个整数:列表中的空元素导致线程中出现异常;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围,java,list,exception,indexing,indexoutofboundsexception,Java,List,Exception,Indexing,Indexoutofboundsexception,我正在处理一个CSV文件,看起来像这样 正如您在冒号“date_Sald”中看到的,存在缺口 我正在尝试将日期缩短为月份(使用此方法,整个日期的前两个整数: public static void getTotalSales(){ int w = 0; while (w < date_sold.size()){ // if the current element is smaller than the size of the if((date_sol
public static void getTotalSales(){
int w = 0;
while (w < date_sold.size()){
// if the current element is smaller than the size of the
if((date_sold.get(w)) != "" ){
// if the element in the array is empty (might be the problem)
date_sold.set(w,((date_sold.get(w)).substring(0,2)));
// trim the string at the element to two characters
}
w = w + 1;
}
System.out.println(date_sold);
}
我想跳过空元素,但不能删除它们,因为空元素的索引对我来说很重要可能的重复不是我的问题
date\u sall.get(w))!=“
但您正在将字符串与进行比较=代码>,无效。此外,在执行子字符串之前,您应该检查长度()
是否至少为2。您可能还需要trim()
字符串。date\u sall.get(w))。子字符串(0,2)
要求字符串长度至少为2。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range