从逗号分隔的字符串作为输入尝试循环遍历数组列表时发生错误。

huangapple go评论75阅读模式
英文:

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 &quot;main&quot; 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(&quot;Enter the shipment details :&quot;);
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(&quot;,&quot;);
//System.out.println(Arrays.toString(userDetailParts));
Shipment shipment = new Shipment();
SimpleDateFormat sdf = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;); 
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(&quot;Enter the number of shipment status :&quot;);
int  n = sc.nextInt();
SimpleDateFormat sdf1 = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;); 
for (int i=1;i &lt;= n;i++) {
System.out.println(&quot;Enter the shipment status &quot; +i+ &quot; details :&quot;);
String userDetail1 = sc.next();
String userDetailParts1[] = userDetail1.split(&quot;,&quot;);
ShipmentStatus SS= new ShipmentStatus();
for (int j = 0; j &lt; 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(&quot;,&quot;);

and:

Hong Kong,Kolkata,17-05-2017,arrived

sc.next() will capture a single string token, hence "Hong".
You wanted sc.nextLine() I think.

huangapple
  • 本文由 发表于 2020年8月18日 15:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63464109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定