Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:07:58+00:00 2026-06-13T15:07:58+00:00

The program runs successfully, but once the prompt for the user to re-run the

  • 0

The program runs successfully, but once the prompt for the user to re-run the program again at the end using the while loop that encapsulates the entire program it throws a NoSuchElementException and despite reading several threads on this forum I can’t understand why. Any help would be much appreciated.

import java.util.Scanner;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;

public class billingStatement {

public static void main(String[] args) {
    String again="y";
    while (again.equalsIgnoreCase("y"))
    {
        //Declare Variables
        String userName="", dateIn="";
        int month=0, date=0, year=0;

        // Billing Statement Header
        System.out.println("Southwest Power and Light");
        System.out.println("Billing Statement");

        //Date, Create Template, Print Result
        Date now = new Date();              
        SimpleDateFormat todaysDate = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println("\n"+"Date: " + todaysDate.format(now));

        //Initialize Scanner
        Scanner scan = new Scanner(System.in);

        boolean validName = false;
        while (validName!= true)
        {
            System.out.print("Please enter your name (Last, First): ");
            try
            {
                userName = scan.nextLine();
                validName = true;
            }
            catch (Exception invalidName)
            {
                int loopCount=0;
                loopCount++;
                System.out.println("Unexpected input type. Please enter a valid name.");
                if (loopCount==2) validName = true;
            }
        }

        // Loop prompt until input's valid
        boolean validDate = false;
        while (!validDate)
        {
            try
            {
                System.out.print("Meter reading date (mm/dd/yyyy): ");
                dateIn = scan.nextLine();               
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                sdf.setLenient(false);
                sdf.parse(dateIn);
                validDate = true;
            }
            catch (Exception invalidDate)
            {
                System.out.println("Unexpected input. Please enter a valid date.");
            }
        }

        // Use Delimiter
        Scanner scanDate = new Scanner(dateIn);
        scanDate.useDelimiter("/");
        month = scanDate.nextInt();
        date = scanDate.nextInt();
        year = scanDate.nextInt();
        scanDate.close();

        //Meter Reading User Input
        double powerUsed = 0;
        boolean validDouble = false;
        while (!validDouble)
        {
            try
            {
                Scanner scanD = new Scanner(System.in);
                System.out.print("Electricity used (KW): ");
                powerUsed = scanD.nextDouble();
                validDouble = true;
                scanD.close();
            }
            catch (Exception invalidDouble)
            {
                int loopCount=0;
                loopCount++;
                System.out.println("Unexpected input. Please enter a valid number.");
                if (loopCount==2) validDouble = true;
            }
        }

        //Calculate base rate via Meter Read Date
        double baseRate = 0;
        switch (month)
        {
            case 1: //January
                baseRate=0.10;break;
            case 2: //February
                baseRate=0.10;break;
            case 3: //March
                baseRate=0.12;break;
            case 4: //April
                baseRate=0.12;break;
            case 5: //May
                baseRate=0.12;break;
            case 6: //June
                baseRate=0.15;break;
            case 7: //July
                baseRate=0.15;break;
            case 8: //August
                baseRate=0.15;break;
            case 9: //September
                baseRate=0.15;break;
            case 10: //October
                baseRate=0.15;break;
            case 11: //November
                baseRate=0.15;break;
            case 12: //December
                baseRate=0.10;break;
        }

        //Currency Format
        NumberFormat currency = NumberFormat.getCurrencyInstance();

        double totalCharge = 0;
        double baseLineCharge = 0;
        double baseCharge = (baseRate*powerUsed);           
        if(powerUsed<350)
        {
            baseLineCharge = powerUsed*baseRate;
        }
        if(powerUsed>350)
        {
            baseLineCharge = 350*baseRate;
        }       
        //Calculate Total Monthly Charge for Power>350 KW
        if (powerUsed<350)
        {
            totalCharge = baseCharge;
        }
        //Calculate Total Monthly Charge for 500 KW>Power>350 KW
        if (powerUsed>350 & powerUsed<500)
        {
            totalCharge = ((baseRate*350)+((powerUsed-350)*(baseRate*1.10)));
        }
        //Calculate Total Monthly Charge for Power>500 KW
        if (powerUsed>500)
        {
            double pieceChargeOne = (baseRate*350);
            //System.out.println(currency.format(pieceChargeOne));
            double pieceChargeTwo = ((150)*(baseRate*1.10));
            //System.out.println(currency.format(pieceChargeTwo));
            double pieceChargeThree = ((powerUsed-500)*(baseRate*1.25));
            //System.out.println(currency.format(pieceChargeThree));
            totalCharge = pieceChargeOne+pieceChargeTwo+pieceChargeThree;               
        }               

        //Print Output
        System.out.println("\nName: "+ userName);
        System.out.println("Meter Reading Date: " + month + "/" + date + "/" + year);
        System.out.println("Electricity Used (KW): "+ powerUsed);

        System.out.println("Baseline Charge: "+ currency.format(baseLineCharge));
        //System.out.println("Over base Charge: "+currency.format(((powerUsed-350)*(baseRate*1.10))));
        System.out.println("Total Amount Due: "+ currency.format(totalCharge));

        // Prompt user for calculating another bill
        Scanner scanAgain = new Scanner(System.in);
        System.out.print("Calculate another bill (y/n)? ");
        again = scanAgain.nextLine();

        scanAgain.close();
        scan.close();
    }
}   

}

This is the block that creates the NoSuchElementException. The scanAgain scanner won’t read in the System.out.print line above it. Sad day.

        // Prompt user for calculating another bill
        Scanner scanAgain = new Scanner(System.in);
        System.out.print("Calculate another bill (y/n)? ");
        again = scanAgain.nextLine();

        scanAgain.close();
        scan.close();
    }
}   

}

the exception

Exception in thread "main" java.util.NoSuchElementException:
No line found at java.util.Scanner.nextLine(Unknown Source) at 
billingStatement.main(billingStatement.java:173) –
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:07:59+00:00Added an answer on June 13, 2026 at 3:07 pm

    Closing (e.g. scanDate.close();) the Scanner closes the underlying stream (System.in) also. You should not do that when you are not yet done with reading from the stream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I developed a model using Z3 .Net API. The program runs well. But when
My app runs successfully in a simulator, but crashes when run on device with
I have the below mail program. The problem is mail program runs successfully but
I wrote my first program in Haskell today. It compiles and runs successfully .
Program runs once and it both throws data to the pipe and gets it
This program runs just fine, but for some reason when I ask for an
I want a program that runs continually and monitors the time constantly. When it's
I have a program that's run in unix (that I have no control over)
When I run the program (server.exe) with Visual C++ 2010 Express's debugger, it runs
I have a client/server program in c. While the server runs, I can send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.