Results 1 to 2 of 2
  1. #1
    tabishis is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Question java.util.NoSuchElementException

    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)

    Java Code:
    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.

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

    Default Re: java.util.NoSuchElementException

    You are trying to read a value from the file when there isn't one. For example the file has 10 numbers and your code tries to read 11 numbers. One way to avoid this is to use the hasNextInt method.

Similar Threads

  1. Replies: 11
    Last Post: 09-04-2012, 05:33 AM
  2. java.util.NoSuchElementException
    By Parkuss in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 08:33 AM
  3. Replies: 6
    Last Post: 03-11-2011, 04:53 AM
  4. Replies: 5
    Last Post: 02-27-2009, 06:19 AM
  5. Replies: 5
    Last Post: 05-14-2008, 02: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
  •