-1

I am having difficulty with this line of code. The code is to calculate a bill of internet service where Package A cost $9.95 a month for ten hours and additional hours for $2.00. Package B cost 13.95 a month for 20 hours with the additional cost of $1.00. Package C cost $19.95 per month. I keep getting this error java.util.NoSuchElementException after I put in the hours.

Here is the code:

import java.util.Scanner;

public class InternetServiceProvider {
    public static void main (String args[])
    {   
        while (true)
        {
            printMonthlyBill(calculateBill(getHours(), menu()));
        }
    }

    public static double getHours()
    {
        double hours;

        Scanner inputHours = new Scanner (System.in);

        System.out.print("Please enter the hours used: ");
        hours = inputHours.nextDouble();

        inputHours.close();

        return hours;
    }

    public static int menu ()
    {
        int packageChoice;

        Scanner userInput = new Scanner (System.in);

        System.out.println("Which package have you obtain? (Please use A, B, or C)");
        System.out.println("[1] Package A");
        System.out.println("[2] Package B");
        System.out.println("[3] Package C");
        System.out.print("Please select your package: ");
            packageChoice = userInput.nextInt();
        userInput.close();

        return packageChoice;
    }
}

This is the input:

    Please enter the hours used: 25
Which package have you obtain? (Please use A, B, or C)
[1] Package A
[2] Package B
[3] Package C
Please select your package: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at InternetServiceProvider.menu(InternetServiceProvider.java:37)
    at InternetServiceProvider.main(InternetServiceProvider.java:8)
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • 1
    Welcome to StackOverflow. This is actually a duplicate question with an answer here: http://stackoverflow.com/q/13042008/1646783. – jlars62 Sep 16 '16 at 22:48

1 Answers1

-1

You are closing your System.in. You should instead share a Scanner between your two methods and close after you are done.

public static void main (String args[])
{   
    try(Scanner input = new Scanner (System.in)) {
       while (true)
       {
           printMonthlyBill(calculateBill(getHours(input), menu(input)));
       }
    }
}

public static double getHours(Scanner input)
{
    double hours;



    System.out.print("Please enter the hours used: ");
    hours = input.nextDouble();

    return hours;
}

public static int menu (Scanner input)
{
    int packageChoice;

    System.out.println("Which package have you obtain? (Please use A, B, or C)");
    System.out.println("[1] Package A");
    System.out.println("[2] Package B");
    System.out.println("[3] Package C");
    System.out.print("Please select your package: ");
        packageChoice = input.nextInt();

    return packageChoice;
}
Manuel Vieda
  • 296
  • 5
  • 12
blue
  • 539
  • 3
  • 7