0

The question is as follows:

Greendart Shipping Company was seen bustling with activities as it was Christmas Weekend and they had numerous shipments to be transported. Ashok, the Shipper had instructed his team to load the shipments into cargos and gave them a list that contained all the shipment names and the corresponding cargo Id through which it is to be shipped. The shipments are usually loaded sequentially, that is first shipment is boarded to first cargo, second shipment to second cargo, etc., During the day break, Ashok wished to manually supervise his team's work. He randomly pulled out a shipment name and verified if it is boarded in the correct Cargo. Since this task was not so efficient, he wanted the Shipment Management System to supervise it automatically. Given the Cargo Id, he wanted the System to give him the name of the shipment boarded into it. If there is no shipment in a cargo, it should print "Yet to be shipped".

Input Format : First input corresponds to the number of Cargos. Second set of inputs corresponds to the Cargo ids. Third input corresponds to the number of shipments. Fourth set of inputs corresponds to shipment names. Fifth input corresponds to cargo id to be searched. Output Format: Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and rest corresponds to output.] Sample Input and Output 1: Enter the number of cargo 4 Enter the cargo id

100

101

102

103

Enter the number of shipment

4

Enter the shipment name

Electric Fan

Air Conditioner

Ceiling Fan

Camera

Enter the cargo id

102

Ceiling Fan

Sample Input and Output 2:

Enter the number of cargo

3

Enter the cargo id

123

124

125

Enter the number of shipment

2

Enter the shipment name

Printer

Xerox Machine

Enter the cargo id

125

Yet to be shipped

My code so far is:

import java.util.*;

public class Main{
public static void main(String args[]) throws Exception{

Scanner reader = new Scanner(System.in);

System.out.println("Enter the number of cargo");
int n = reader.nextInt();

int[] cargoid = new int[n];

System.out.println("Enter the cargo id");
//get cargo ids
for(int i=0; i<n; i++){
cargoid[i] = reader.nextInt();
System.out.println(i +" " +cargoid[i]);
}

//get shipment names
System.out.println("Enter the number of shipment");
int m = reader.nextInt();
String[] shipname = new String[m];

System.out.println("Enter the shipment name");
for(int j=0; j<m+1; j++){
shipname[j]=reader.nextLine();
System.out.println(j +" " +shipname[j]);
}

I find that shipname starts from index 1 rather than 0. with the current code, the editor simulates results. Attaching screenshot herescreenshot

  • This is almost certainly do the behavior of `Scanner`. My guess is that there is a carriage return which is being carried over somewhere, making it appear that an input is disappearing. – Tim Biegeleisen May 13 '18 at 03:12
  • Yes, I tried using next() initially, but, that splits with every space and takes each word into each index – user1627288 May 13 '18 at 03:15
  • I have run your program online and it is not executing after printing cargo id – Adya May 13 '18 at 03:22
  • yes the program is not complete. I have issues with //get shipment names System.out.println("Enter the number of shipment"); int m = reader.nextInt(); String[] shipname = new String[m]; System.out.println("Enter the shipment name"); for(int j=0; j – user1627288 May 13 '18 at 03:26
  • I have attached screenshot. The editor I use simulates based on my code and If you can see that, shipment names start from index 1 rather than 0 . Please suggest a solution for that – user1627288 May 13 '18 at 03:28
  • Answered here: https://stackoverflow.com/a/13102066/1110636 – Timir May 13 '18 at 03:46

0 Answers0