Results 1 to 7 of 7
- 03-11-2011, 02:59 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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
- 03-11-2011, 03:05 AM #2
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.
- 03-11-2011, 03:08 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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
- 03-11-2011, 03:15 AM #4
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.
- 03-11-2011, 03:25 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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.
-
Last edited by Fubarable; 03-11-2011 at 03:41 AM.
- 03-11-2011, 03:53 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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
-
Need help with 'Exception in thread "main" java.util.NoSuchElementException'
By Wicked in forum New To JavaReplies: 11Last Post: 02-11-2011, 12:11 AM -
Exception in thread "main" java.util.NoSuchElementException
By xx__rose in forum New To JavaReplies: 4Last Post: 05-07-2010, 07:58 PM -
Exception in thread "main" java.util.NoSuchElementException
By vileoxidation in forum New To JavaReplies: 5Last Post: 09-17-2008, 07:29 AM -
Exception in thread "main" java.util.NoSuchElementException
By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 02:19 PM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks