英文:
Error while trying to loop arraylist from a comma separated string as input
问题
我遇到错误:`Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Section5.TestMain.main(TestMain.java:51)`
public class TestMain {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("输入装运详情:");
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
//System.out.println(Arrays.toString(userDetailParts));
Shipment shipment = new Shipment();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
shipment.setid(userDetailParts[0]);
shipment.setsourcePort(userDetailParts[1]);
shipment.setdestinationPort(userDetailParts[2]);
shipment.setexpectedDeliveryDate(sdf.parse(userDetailParts[3]));
shipment.setcustomerName(userDetailParts[4]);
System.out.println("输入装运状态数量:");
int n = sc.nextInt();
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
for (int i=1;i <= n;i++) {
System.out.println("输入装运状态 " +i+ " 详情:");
String userDetail1 = sc.next();
String userDetailParts1[] = userDetail1.split(",");
ShipmentStatus SS= new ShipmentStatus();
for (int j = 0; j < userDetailParts1.length; j++) {
SS.setarrivalPort(userDetailParts1[0]);
SS.setdeparturePort(userDetailParts1[1]); // 此行出现错误
SS.setarrivedDate(sdf1.parse(userDetailParts1[2]));
SS.setdstatus((userDetailParts1[3]));
}
//System.out.println(Arrays.toString(userDetailParts1));
}
ShipmentBO bo = new ShipmentBO();
bo.displayStatusOfShipment(shipment);
}
}
输入和错误如下:
> 输入装运详情:
> STAJU01,Hong Kong,Cochin,20-05-2017,karthick
> 输入装运状态数量:
> 3
> 输入装运状态 1 详情:
> Hong Kong,Kolkata,17-05-2017,arrived
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Section5.TestMain.main(TestMain.java:51)
英文:
I am getting error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Section5.TestMain.main(TestMain.java:51)
public class TestMain {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the shipment details :");
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
//System.out.println(Arrays.toString(userDetailParts));
Shipment shipment = new Shipment();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
shipment.setid(userDetailParts[0]);
shipment.setsourcePort(userDetailParts[1]);
shipment.setdestinationPort(userDetailParts[2]);
shipment.setexpectedDeliveryDate(sdf.parse(userDetailParts[3]));
shipment.setcustomerName(userDetailParts[4]);
System.out.println("Enter the number of shipment status :");
int n = sc.nextInt();
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
for (int i=1;i <= n;i++) {
System.out.println("Enter the shipment status " +i+ " details :");
String userDetail1 = sc.next();
String userDetailParts1[] = userDetail1.split(",");
ShipmentStatus SS= new ShipmentStatus();
for (int j = 0; j < userDetail1.length(); j++)
{
SS.setarrivalPort(userDetailParts1[0]);
SS.setdeparturePort(userDetailParts1[1]); //error is observed on this line
SS.setarrivedDate(sdf1.parse(userDetailParts1[2]));
SS.setdstatus((userDetailParts1[3]));
}
//System.out.println(Arrays.toString(userDetailParts1));
}
ShipmentBO bo = new ShipmentBO();
bo.displayStatusOfShipment(shipment);
}
}
input and error as below:
>Enter the shipment details :
>
>STAJU01,Hong Kong,Cochin,20-05-2017,karthick
>
>Enter the number of shipment status :
>
>3
>
>Enter the shipment status 1 details :
>
>Hong Kong,Kolkata,17-05-2017,arrived
>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Section5.TestMain.main(TestMain.java:51)
答案1
得分: 0
问题在这里:
String userDetail1 = sc.next();
String userDetailParts1[] = userDetail1.split(",");
以及:
Hong Kong,Kolkata,17-05-2017,arrived
sc.next() 将会捕获一个单独的字符串标记,因此是 "Hong"。
我认为你想要的是 sc.nextLine()。
英文:
The problem is here:
String userDetail1 = sc.next();
String userDetailParts1[] = userDetail1.split(",");
and:
Hong Kong,Kolkata,17-05-2017,arrived
sc.next() will capture a single string token, hence "Hong".
You wanted sc.nextLine() I think.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论