Results 1 to 13 of 13
  1. #1
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default xception in thread "main" java.util.NoSuchElementException: No line found

    this is the exception the code is throwing when it runs:

    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Payroll3.inputData(Payroll3.java:229)
    at Payroll3.main(Payroll3.java:24)

    This is the InputFile thats being used

    John Smith
    9.45 40 15
    Jane Doe
    12.50 45 15
    Harry Morgan
    20.00 40 20
    Carmen Martinez
    25.00 35 25
    Jacintha Washington
    50.85 60 34

    The two methods that are being used inside the loop to print out the information are

    public static boolean inputData(Scanner inFile, StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate) //name and formal parameters to inputData
    {//begining of inputData
    if(!inFile.hasNext())
    return false;
    else
    {
    employeeName.setLength(0);
    employeeName.append(inFile.nextLine());
    hourlyRate.setNum(inFile.nextDouble());
    hoursWorked.setNum(inFile.nextDouble());
    taxRate.setNum(inFile.nextDouble());
    inFile.nextLine();

    return true;
    }
    }//end of inputData

    public static void printEmployeeInfo(StringBuffer employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount) //printEmployeeInfo and its paramaters
    {//start of printEmployeeInfo
    final double FULL_TIME=40.0;
    int count, blanks=69;
    for(count=0; count<=blanks; count++)//for loop intialization, termination, counter
    {//begin for loop
    if(count==0)
    System.out.printf("%-19s",employeeName);
    if(count>19 && count<22)
    System.out.print(" ");
    if(count==22)
    System.out.printf("%8.2f", hourlyRate); //right justification of double parameter
    if (count>29 && count<32) //spaces
    System.out.print(" ");
    if(count==32) //right justification of double parameter
    System.out.printf("%8.2f", hoursWorked);
    if (count>39 && count<42) //spaces
    System.out.print(" ");
    if(count==42) //right justification of double parameter
    System.out.printf("%8.2f", taxRate);
    if (count>49 && count<52) //spaces
    System.out.print(" ");
    if(count==52) //right justification of double parameter
    System.out.printf("%8.2f", grossAmount);
    if(count>59 && count<62) //spaces
    System.out.print(" ");
    if(count==62) //right justification of double parameter
    System.out.printf("%8.2f", netAmount);
    if (count==blanks && hoursWorked>FULL_TIME)
    System.out.print(" OT");
    if(count==blanks) //new line
    System.out.println();
    }//end for loop
    }//end of printEmployeeInfo

    Now what I can see that it is doing, with the input file is its skipping over the second name and information and going to the third, and going by that theory it should print up the last name and set of information but its not.

    this is a program thats a project for my class so its using the pieces of Java that are instructed and the print method works, it was written for the part before this particular program its the Boolean that's not working (I think.)

    Here's the code for the program itself

    public static void main (String []args) throws FileNotFoundException//main
    { //start of Main arguments
    Scanner inFile = new Scanner (new FileReader("payrolldat.txt")); //what will change depending on the input format
    StringBuffer employeeName = new StringBuffer(); //variables declared for use of printEmployeeInfo method in 7.5 before payrollprocessing code
    double grossAmount=0.0, netAmount=0.0;
    DoubleClass hourlyRate=new DoubleClass();
    DoubleClass hoursWorked=new DoubleClass();
    DoubleClass taxRate=new DoubleClass();
    boolean moreData=true;
    instructions(); //method call to instructions

    reportTitle();//method call to reportTitle
    while (moreData) //while condition
    {//enter while loop
    moreData=inputData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);

    printEmployeeInfo(employeeName, hourlyRate.getNum(), hoursWorked.getNum(), taxRate.getNum(),grossAmount, netAmount); //call to printEmployeeInfo

    moreData=inputData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);
    }//end while
    } //end main

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

    Default

    This error usually happens if you call Scanner#next() without checking hasNext first. Remember you should check this or the equivalent Scanner#hasNextXXX() before every call to Scanner#nextXXX(). You're not doing this. You only check hasNextXXX() at the beginning of the loop.

  3. #3
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    it has an if statement in it to check if it hasNext before it gets the information its looking to make sure that there's a string set there o.O and it does it...twice...

  4. #4
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    ok So I put the if(inFile.hasNext()) before each statement and it now doesn't have the error but it still picks up only three of the people, it skips one between each...

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

    Default

    your while loop needs work.
    Java Code:
    while (moreData)
    {
      moreData = getSomeData();
      doSomethingWithData();
      moreData = getSomeData();  // and just ignore it, don't work on it.
    }
    This logic is faulty. Can you see why it's skipping lines here (with scrutiny it should be obvious)? You can think of a better while loop, right? give it a try.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  6. #6
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    lol, that was the logic given by the teacher for how to write the loop. That is why it was used. But...I will see.

  7. #7
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    AHhh now it works.

    You have been an immense help thank you very very much.

  8. #8
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    my guess without debugging it is that the problem is you are calling your inputData method a second time inside that loop. this increments the scanner with inFile.nextLine() but never prints anything and then you increment again at the start of the next loop.

    Edit: Fubarable beat me to it.

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

    Default

    my guess without debugging...
    that's what I'm trying to get him to see. There are more problems with this code, but that's the main one.

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

    Default

    To the original poster, let me know if you have this fixed. You have more problems and I'm going to bed soon.

  11. #11
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    I just said it worked O.O;

    Sorry if I didn't make it clear, its solved, and thank you for the point out on the loop. that was a teacher mistake I'll tell her about it tomorrow when I turn it in

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

    Default

    You might want to use two Scanner objects here, one for your "inFile" and the other a temporary Scanner object created in the inputData method. Again, inFile should be checked for hasNextLine() before any line is read into it. The first line will be used to get the name, the second line should be read in its entirety into a String. This String can then be parsed by the second temporary or short-lived Scanner object, or you could do a String.split(" "); using a space, " ", as the delimiter then parse each item in the array using Double.parseDouble() method.

    If you use a Scanner object for the second line, don't forget to close it when you're done with it. Again, you should check it with hasNextDouble() before trying to extract a double.

    Good night and good luck.

  13. #13
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    I believe sense it works and it does what its supposed to that that part is finished, This is all due by like midnight tomorrow and beyond the last two pieces of this lab in paritcular I have two others to do, so sense this paritculer one does what its supposed to do, gets all the values etc. I'm going to move on to finish it. This break when I have time to play with it, I'll try that though.

    Thanks.

Similar Threads

  1. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  2. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  3. Replies: 5
    Last Post: 05-14-2008, 01:43 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •