Results 1 to 2 of 2
  1. #1
    sjan is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default runProgram Exception in Thread

    Hello All! I had a quick question about some code I'm working on. The goal of the program is to ask for several customer ID's along with how many kilowatt-hours they use in electricity so that their monthly charge can be calculated. However, my main concern is the runProgram variable. When entering the first customer's set of data, the program works smoothly. However, when it goes to ask for the next customer's set of data it displays this:

    Console Run:
    "Please enter Y to calculate another bill or Q to quit.
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at edu.janaskie.CostOfElectricity.main(CostOfElectric ity.java:58)"

    I've tried to figure it out but obviously I haven't been successful.
    Here is my code-
    Java Code:
    Java Code:
    package edu.janaskie;
    
    import java.util.Scanner;
    
    public class CostOfElectricity {
    	
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner (System.in);
    		
    				String customerID = "";
    				char runProgram = ' ';
    				double kWh = 0.0;
    				double charge = 0.0;
    				int totalCustomers = 0;
    				double totalKwH = 0.0;
    				double averageKwH = 0.0;
    				double totalCharges = 0.0;
    				
    			System.out.println("Electricity Rate Summary Program");
    			System.out.println("================================");
    			System.out.println("For this program you will need to enter customer ID (three numbers only) followed usage of kilowatt-hours");
    			System.out.println("Enter a Y to start the prgram and a Q to quit.");
    			
    			runProgram = input.nextLine().charAt(0);
    			runProgram = Character.toUpperCase(runProgram);
    			
    			while(runProgram != 'Q' && runProgram != 'Y')
    			{
    				System.out.printf("\n%s", "This is an invalid entry. Please enter Y to calculate another electric bill or Q to quit.");
    				runProgram = input.nextLine().charAt(0);
    				runProgram = Character.toUpperCase(runProgram);
    			}
    			
    			while (runProgram == 'Y')
    			{
    				
    				customerID = getCustID();
    				
    				kWh = getCustKwh();
    				
    				charge = getCharge(kWh);
    				
    				displayCustData(customerID, kWh, charge);
    				
    				
    				System.out.println();
    				System.out.println();
    				System.out.println();
    				System.out.println("Please enter Y to calculate another bill or Q to quit.");
    				runProgram = input.nextLine().charAt(0);
    				runProgram = Character.toUpperCase(runProgram);
    				
    				while(runProgram != 'Q' && runProgram != 'Y')
    				{
    					System.out.printf("\n%s", "This is an invalid entry. Please enter Y to calculate another electric bill or Q to quit.");
    					runProgram = input.nextLine().charAt(0);
    					runProgram = Character.toUpperCase(runProgram);
    				}
    				
    			}
    			
    				totalCustomers = totalCustomers + 1;
    				totalCharges = totalCharges + charge;
    				totalKwH = totalKwH + kWh;
    				averageKwH = totalKwH / totalCustomers;
    
    				displayFinalReport(totalCustomers, totalKwH, totalCharges, averageKwH);
    			
    			
    				
    				input.close();
    	}
    
    				public static String getCustID()
    				{
    					Scanner input = new Scanner (System.in);
    					
    					String aCustID = "";
    					
    					System.out.println("Enter customer ID");
    					aCustID = input.nextLine();
    					
    					return aCustID;
    				}
    				
    				public static Double getCustKwh()
    				{
    					Scanner input = new Scanner (System.in);
    					
    					double aCustKwh = 0.0;
    					
    					System.out.println("Please enter the total usage of KWH for the customer:");
    					aCustKwh = input.nextDouble();
    					input.nextLine();
    					
    					return aCustKwh;
    				}
    				
    				public static Double getCharge(double aCustKwh)
    				{
    					Scanner input = new Scanner (System.in);
    					
    					double aCharge = 0.0;
    					
    					if (aCustKwh < 300)
    					{
    						aCharge = aCustKwh * .12;
    					}
    					else if (aCustKwh < 600)
    					{
    						aCharge = ((aCustKwh - 300) * .09) + (300 * .12);
    					}
    					else if (aCustKwh < 1000)
    					{
    						aCharge = ((aCustKwh - 600) * .06) + (300 * .09) + (300 * .12);
    					}
    					else if (aCustKwh > 1000)
    					{
    						aCharge = ((aCustKwh - 1000) * .04)+ (600 * .06) + (300 * .09) + (300 * .12);
    					}
    					input.close();
    					
    					return aCharge;
    					
    					
    				}
    				
    				public static void displayCustData( String aCustID, double aCustKwh, double aCharge)
    				{
    					Scanner input = new Scanner (System.in);
    					
    					System.out.println();
    					System.out.println();
    					System.out.println();
    					System.out.println("FINAL CUSTOMER TOTAL FOR ELECTRICITY USAGE");
    					System.out.println("CUST ID			KWH			CHARGE($)");
    					System.out.println("-------			---			---------");
    					System.out.printf("%s 			%.2f 			%.2f%n", aCustID, aCustKwh, aCharge); 
    
    				}
    				
    				public static void displayFinalReport(int totalCustomers, double totalKwH, double totalCharges, double averageKwH)
    				{
    					if (totalCustomers == 0)
    					{
    						System.out.println("No bills were calculated.");
    					}
    					
    					else
    					{
    						System.out.printf("\n%-60s%20d\n", "Total Customers: ", totalCustomers);
    					}
    					
    					if (totalKwH == 0)
    					{
    						System.out.println("No total kilowatt-hours were calculated");
    					}
    					
    					else
    					{
    						System.out.printf("\n%-60s%20f\n", "Total Kilowatt-Hours: ", totalKwH);
    					}
    					
    					if (averageKwH == 0)
    					{
    						System.out.println("No average kilowatt-hours were calculated.");
    					}
    					
    					else
    					{
    						System.out.printf("\n%-60s%20f\n", "Average Kilowatt-Hours: ", averageKwH);
    					}
    					
    					if (totalCharges == 0)
    					{
    						System.out.println("No total charges calculated.");
    					}
    					
    					else
    					{
    						System.out.printf("\n%-60s%20.2f\n", "Total Charges: ", totalCharges);
    					}
    				
    					System.out.println("Thank you so much for using Electricity Rate Summary! Have a nice day.");
    
    				}
    }

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: runProgram Exception in Thread

    Using more than one instance of the Scanner class may be causing a problem. Try creating one instance and using it in the rest of the code.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 12-17-2013, 05:22 AM
  2. Exception in Thread..
    By aabdi406 in forum New To Java
    Replies: 2
    Last Post: 04-12-2011, 08:02 PM
  3. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  4. Exception in thread main
    By maya700 in forum New To Java
    Replies: 14
    Last Post: 06-27-2010, 07:59 PM
  5. Replies: 3
    Last Post: 11-06-2008, 04:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •