-1

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)

jhamon
  • 3,346
  • 3
  • 23
  • 35
  • Try using **String userDetail1 = sc.nextLine();** instead of String userDetail1 = sc.next();. next will stop at Hong because of the space that follows it. – Kasalwe Aug 18 '20 at 07:59
  • useful: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – jhamon Aug 18 '20 at 08:00

1 Answers1

0

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.

John
  • 629
  • 3
  • 11
  • And to face problem after that change see [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo Aug 18 '20 at 08:00
  • Yes, the unconsumed \n arising from ```int n = sc.nextInt();``` – John Aug 18 '20 at 08:03
  • and then the loop condition is bad (and the loop is useless) – jhamon Aug 18 '20 at 08:11
  • Are we trying to fix every problem in his program or just answer his question? – John Aug 18 '20 at 08:30
  • if i use sc.nextLine() the output is displayed 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 : Enter the shipment status 2 details : – user1677627 Aug 18 '20 at 13:43
  • i am not able enter details for shipment status 1 – user1677627 Aug 18 '20 at 13:45