Java Project java.util.nosuchelementexception?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java Project java.util.nosuchelementexception?

  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Java Project java.util.nosuchelementexception?

    Hey guys, new to this forum...but waited too long to do my project...I did some things differently than most would, just to practice with formatting.

    But when I run this, I get
    exception in thread main java.util.nosuchelementexception with some numbers after
    Code:
    /**********************************************************************************
    *  Name: Chris Rosswog                                                            *
    *  Due date: April 10, 2011 @ 11:59 PM                                            *
    *  Program Number: 3                                                              *
    *                                                                                 *
    *  Compilation:  javac Sales.java                                                 *
    *  Execution:    java Sales<test.dat                                              *
    *                                                                                 *
    * -----------------------------------------------------------                     *
    * ID          TOTAL        NET          PROFIT     COMMISSION                     *
    * NUMBER      SALES(A)     SALES(B)     (A - B)    13&#37;(A - B)                     *
    * -----------------------------------------------------------                     *
    * KM2506      1097.52       798.52      299.00         38.87                      *
    * SL1561       745.10       700.10       45.00          5.85                      *
    * RT8109       568.19       520.19       48.00          6.24                      *
    * WQ4178       772.00       689.00       83.00         10.79                      *
    * -----------------------------------------------------------                     *
    * Totals      3182.81      2707.81      475.00         61.75                      *
    *  ----------------------------------------------------------                     *
    *                                                                                 *
    **********************************************************************************/
    
    import java.util.*;
    import java.lang.Math;
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //public class that calls out the methods used in the program
    public class Sales
    {
    
    	//This allows these variables to exist throughout the entire program.
    	static Scanner keyboard = new Scanner(System.in);
    	static double totalsalesa, totalsalesb, totalprofit, totalcommission;
    
    
    	public static void main(String[] args)
      {
    
    	Scanner keyboard = new Scanner(System.in);
    
    	//Makes the heading of the program
         Heading();
         //Skips first line of data file which should be labeled
         keyboard.nextLine();
         //While loop until data runs out
         while(keyboard.hasNext())
    	 	{
    	 			Display();
    		}
    
         String TotalLabel = "Totals";
    
         //Display running totals for everything
    	 System.out.println("-----------------------------------------------------------");
    	 System.out.printf("%-8.2s %12.2f 13.2f 11.2f 14.2f \n", TotalLabel, totalsalesa, totalsalesb, totalprofit, totalcommission);
    	 System.out.println("-----------------------------------------------------------");
    
    
       }
    
       ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       //Main method above
       ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       //Below is the formatting for the heading
    
       public static void Heading()
       {
    	//Here I started declaring the strings for my header to be used in formatted output. L stand for label...
    	  String ID = "ID";
    	  String TOTALL = "TOTAL";
    	  String NETL = "NET";
    	  String PROFITL = "PROFIT";
    	  String COMMISSIONL = "COMMISSION";
    	  String NUMBERL = "NUMBER";
    	  String SALESAL = "SALES (A)";
    	  String SALESBL = "SALES (B)";
    	  String ABL = "(A - B)";
    	  String CL = "13%(A - B)";
    
    	//I took an approach in which everything is not determined by simply my spacing (imperfect).
    	//Instead, I decided to using formatting output for the header also.  Words can be easily manipulated without messing
    	//up spacing.
          System.out.printf("%-12s %-13s %-13s %-11s %-10s \n", ID, TOTALL, NETL, PROFITL, COMMISSIONL);
          System.out.printf("%-12s %-13s %-13s %-11s %-10s \n", NUMBERL, SALESAL, SALESBL, ABL, CL);
          System.out.println("---------------------------------------------------------------");
       }
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    	//Make a method to get the information
    	public static void Display()
    	{
    
    		Scanner keyboard = new Scanner(System.in);
    
    		String idnumber;
    		double commissionvalue = .13;
    		double salesa;
    		double salesb;
    		double profit;
    		double commission;
    
    
    		idnumber = keyboard.next();
    		salesa = Double.parseDouble(keyboard.next());
    		salesb = Double.parseDouble(keyboard.next());
    		profit = (salesa - salesb);
    		commission = (commissionvalue*profit);
    
    		totalsalesa += salesa;
    		totalsalesb += salesb;
    		totalprofit += profit;
    		totalcommission += commission;
    
    		System.out.printf("%-8.2s %12.2f 13.2f 12.2f 14.2f", idnumber, salesa, salesb, profit, commission);
    
    
    	}
    
    
    }
    My data file contains the following:


    Sales Program Data
    KM2506 1097.52 798.52
    SL1561 745.10 700.10
    RT8109 568.19 520.19
    WQ4178 772.00 689.00
    PL8979 2590.65 2140.50


    Any suggestions? It compiles correctly..
    Last edited by Hack; 03-31-2011 at 08:50 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Mar 2011
    Posts
    4
    At a guess ild say its because your opening System.in twice. try keyboard.close(); or whatever it is cant remember it right now. With scanners u cannot open the same file twice or it will give that error so u need to close one scanner before opening it again with another, tell me if it works otherwise ill look at the code and give u an answer,

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. Good Editorial by Russell Jones
    By Robert G in forum .NET
    Replies: 84
    Last Post: 02-08-2001, 03:38 PM
  4. Replies: 0
    Last Post: 09-13-2000, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center