0

I am trying to create a Java program that reads in a set of shipments (using Scanner and a while loop), breaking if shipment number is -1 and then rejects and prints each rejected shipment along with an error message if weight is negative or zero or greater than 70 pounds, or if volume is negative or zero or greater than 1000 cubic inches.

I am receiving the following error message when compiling:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Shipment.main(Shipment.java:30)

I am not sure what I am doing wrong, here is the code I have now:

import java.util.Scanner;  
public class Main {  
    public static void main(String[] args) {  
        int shipmentNumber;  
        double shipmentWeight = 0;  
        double shipmentVolume = 0;  
        double totalWeight = 0;  
        double totalVolume = 0;  
        Scanner sc = new Scanner(System.in);  
        while(true) {  
            System.out.println("Enter shipment number: ");  
            shipmentNumber = sc.nextInt();  

            if(shipmentNumber == -1) {  
                break;  
            }  
            System.out.println("Enter shipment weight: ");  
            shipmentWeight = sc.nextDouble();  
            System.out.println("Enter shipment volume: ");  
            shipmentVolume = sc.nextDouble();  
            if(shipmentWeight <= 0 || shipmentWeight > 70) {  
                System.out.println("Shipment " + shipmentNumber + " rejected!!! Weight '" + shipmentWeight + "'pounds is invalid");  
                continue;  
            } else if (shipmentVolume <= 0 || shipmentVolume > 1000) {  
                System.out.println("Shipment " + shipmentNumber + " rejected!!! Volume '" + shipmentVolume + "'cubic inches is invalid");  
                continue;  
            } else {  
                System.out.println("Shipment " + shipmentNumber + " accepted!!!\nWeight: " + shipmentWeight + "pounds  Volume: " + shipmentVolume + "cubic inches");  
            }  

            totalWeight += shipmentWeight;  
            totalVolume += shipmentVolume;  
        }  
        System.out.println("Total shipment weight: " + totalWeight + "pounds");  
        System.out.println("Total shipment volume: " + totalVolume + "cubic inches");  
    }  
}  
EpicFail18
  • 15
  • 1
  • 5

3 Answers3

1

From the comments, it seems you re running the program in online editor/compiler. Most of the online compilers do not expose Standard input and output to end user. So System.in is not available. Run this code locally in your pc and it will run without issues.

TruckDriver
  • 1,383
  • 13
  • 28
  • ... or if you have to run it online, don't use `System.in` Create a `StringReader` or something like that as your input for testing. – Mike Harris Dec 01 '17 at 15:49
0

Would you like to try the code out to an IDE and use some TDD practices to see where is your code broken? I would change while (true) {break;} into while (shipmentNumber != -1) and stop using continue into the loop, you should simply remove that by using nested if/else or changing the way of doing the loop. However, what is line 30 in your code?

Alter
  • 903
  • 1
  • 11
  • 27
-1

How are you getting that exception on compile? NoSuchElementException is only thrown during runtime.

If you are actually getting the above exception on runtime, then it seems like the input is exhausted and it is unable to assert an integer value before calling nextInt().

Try by following this snippet

if(sc.hasNextInt()){
    shipmentNumber = sc.nextInt();
}
Haagenti
  • 5,602
  • 1
  • 9
  • 17
  • There won't be any compiling issues since this is not a compile-time error. This is an exception thrown at runtime. – Haagenti Dec 01 '17 at 15:52
  • i mean running successfuly without throwing any error or exception – Lalit Verma Dec 01 '17 at 15:53
  • Even i don't see any visible errors in his code, If the exception that he has logged is legit, then the above snippet is something that he should take a look at – Haagenti Dec 01 '17 at 15:59