1 Replies - 1250 Views - Last Post: 07 October 2012 - 08:00 PM Rate Topic: -----

#1 tabishis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-September 12

java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner

Posted 07 October 2012 - 07:43 PM

I'm receiving following error when I try to run my program (it compiles successfully):

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Billing.main(Billing.java:20)

import java.util.Scanner;
import java.io.*;
import java.text.NumberFormat;
public class Billing 
{
public static void main(String [] args) throws IOException 

{
File input = new File("F:\\sprayingdata.txt");
Scanner scan = new Scanner(input); 

int clientNumber,type=1,acres,number,total,counter;
double amountDue=1;
{

clientNumber = scan.nextInt(); 

while(clientNumber < 9999)

type = scan.nextInt();
scan.nextLine(); 
acres = scan.nextInt(); 
scan.nextLine();
scan.close(); 
total = acres = 0;
System.out.println("-----Client Number-----" + "-----Type-----"+ "-----Acres-----"+ "-----Amount Due-----");
System.out.println(("-----" + clientNumber + "-----") + ("-----" + type + "-----")+ ("-----" + acres +"-----") + ("-----" + amountDue + "-----"));

total += type;
acres++;
}
if (clientNumber==0)
{

NumberFormat fmt = NumberFormat.getCurrencyInstance(); 
amountDue = calculateBill(type,acres); 
fmt.format(amountDue);
amountDue = (double) total/acres;
System.out.println("The total bill is: " + acres + " numbers is " + amountDue);
} 

else
System.out.println("Please enter a valid account number.");

}
public static double calculateBill(int type,int acres)
{
int multiplier;
double grossBill;
if (type==1)
multiplier=1;
else if (type==2)
multiplier=3;
else if (type==3)
multiplier=4;
else if (type==4)
multiplier=6;
else
return -1;
grossBill = multiplier * acres;
if (acres > 1000)
grossBill -= 0.05 * grossBill;
if (grossBill > 1500) 
grossBill -= 0.1 * (grossBill - 1500);
return grossBill; 
}
}




I am trying to read following data from a text file using Scanner class:

1000 1 1200
1001 2 800
1002 3 300
1003 6 1100
1004 3 783
1005 1 1550
1006 5 950
1007 1 750
1008 2 1050
1009 2 600
1010 4 700
1011 3 1200
1012 3 0
0 0 0

I think data is not being read from the text file properly. Any hints or clues will be greatly appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner

Posted 07 October 2012 - 08:00 PM

Assuming this is not a copy/paste error.

Lines 18-20:
while(clientNumber < 9999)
    type = scan.nextInt();



will keep reading from the file indefinitely (you are not changing the value of clientNumber, so it will never be more than 9999) and will throw an exception when the end of file is reached.

It would be much easier for you and us to actually indent your code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1