Results 1 to 7 of 7
  1. #1
    dude1it is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default exception in thread main java.util.nosuchelementexception

    I get an exception in thread main java.util.nosuchelementexception
    when I do this...any suggestions?

    /************************************************** ********************************
    * Name: ----- ------- *
    * 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%(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 source file is:


    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 help is appreciated! I procrastinated on a project due over spring break...sooo here I am.
    Last edited by pbrockway2; 04-05-2013 at 11:23 PM. Reason: name removed

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    TL DR

    Your problem is caused by trying to read too much input. Imagine that you have 3 items in your file. Then you try and read 4 items from it. Obivously the fourth time you attempt to read there is nothing left, hence the exception. Now it is your job to find out where and or why you are doing this.

  3. #3
    dude1it is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    The part where I'm reading the input is (the first line is skipped) :

    idnumber = keyboard.next();
    salesa = Double.parseDouble(keyboard.next());
    salesb = Double.parseDouble(keyboard.next());

    I don't really see where you're getting with that :/

    Project due at 12, I really put it off :(

    After which

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by dude1it View Post
    Project due at 12, I really put it off :(
    That is your problem, not mine or anyone else's. I toldf you what your problem is. It is now upto you to find out where the problem lies and then fix it. It is not my responsibility. If you cannot fix it then hand in what you have done and take the grade you deserve.

    Try some poor man's debugging. Insert a heap of print statements all over your code to see what values your variables have and when. Then see if they match your expectations.

  5. #5
    dude1it is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes, my problem, which is why I'm surfing the web and trying to improve what grade I will be getting...

    I have googled, textbooked, I have tried to "poor man" it....

    NOW, I'm at a complete loss, have been working on this piece of crap for about 10 hours (yea, I'm not that great at this). Basically losing patience with everything at this point. Anyone have any idea what specifically could be up with this? I'm sure it's a one statement thing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dude1it View Post
    My source file is:...
    I don't see anywhere in the code you've posted above where you deal with files. Where is this code?

    Edit: no response yet, and time for me to go to bed. To the OP: best of luck on this project.
    Last edited by Fubarable; 03-11-2011 at 03:41 AM.

  7. #7
    dude1it is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your help you two above!
    Sorry for the slow response...was working on it, started by getting rid of a method..

    I copied and pasted a .txt file's contents in the post.

    At this point, I've got it doing what it's needing to do, except showing the numbers after the id that are suppose to show up, which i believe I just need to append the ints to it

Similar Threads

  1. Replies: 11
    Last Post: 02-11-2011, 12:11 AM
  2. Replies: 4
    Last Post: 05-07-2010, 07:58 PM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Posting Permissions

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