• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

No Such element exception error code?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have this code where i am reading in info from a file and i keep getting the loop wrong. right now my code looks like:



And the error message is:



Line 62 is the line where i am reading the info in cost.
 
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you reading from a file or the keyboard? Can we see the line(s) where you create inputScanner?
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What input did you give it?
 
zach pass
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the top part.

Im pretty sure thats fine, but the input file is this

 
zach pass
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry here is the correct first part of code

 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
zach pass
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i thought you just wanted to see how i structured it. This is my FULL code :

 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It read up to X-ray ok but when you try to read the last record you do not have a nextLine().
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 4767
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is why I hate Scanner. It's a bear to debug. The output file should have given you a little clue that everything was ok up until reading the last record.
 
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ZP: Welcome to the Ranch

Carey Brown wrote:This is why I hate Scanner. It's a bear to debug. . . . .

I don't hate it, but it is, as Winston told us a long time ago, not well documented, least of all in the books. Its behaviour differs depending on whether it si reading from a file or from System.in.
One problem is that lots of books tell you that nextLine() reads the next line, which is exactly what it doesn't do. The documentation clearly says it reads the remainder of the current line starting where the reading left off. That loop calls nextLine() five times, which means
  • 1: Each record must occupy exactly five lines
  • 2: The first line in each record is regarded as text called Name. The whole line is used.
  • 3: The 2nd line in each record starts with a double to be used for cost. Anything following that in this line is skipped.
  • 4: The 3rd line in each record starts with an int used for quantity. Anything following in that line is skipped, too.
  • 5: The 4th line in each record is regarded as text and used for isCov. But in your file you have written true or false. That suggests you don't want nextLine() at all, but nextBoolean(), and the variable shouldn't be a String in the first place. Whatever followis in that line is skipped.
  • 6: The 5th line in each record starts with a double which is used for pctCovered. Anything following in that line is skipped.
  • So, let's look at your file and see whether it matches that format. Not even close. You must be reading the first eight lines somewhere else. Then each line number ending 4 or 9 appears to start a new record, until you get to line 44 where there isn't anything.
    Suggestions:-
  • 1: Because the name of the procedure occupies a varying number of “tokens”, you will have to use nextLine() to read it.
  • 1½: The name of the procedure must occupy the whole of the line, i.e. a line to itself.
  • 2: Reading the number 7 in line 8 will leave the Scanner at the end of line 8, not the beginning of line 9, so a nextLine() call is needed to prevent you from reading the remainder of the line as the empty String "" and thereafter getting all the other “tokens” out of sequence.
  • 3: Delete the nextLine() calls in lines 13, 16, and 21. They are not necessary. The default delimiter is whitespace and line ends count as whitespace, so you will move on to those “tokens” anyway.
  • 4: Replace line 19 with nextBoolean() and change the variable's type.
  • Now you have made those changes you won't have to worry about whether the cost and other things are on one line or four lines. As long as you call nextLine() once before the procedure name, all will be well . . . until you read my next post.
     
    Campbell Ritchie
    Marshal
    Posts: 60142
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    A few minutes ago, I wrote:. . . all will be well . . . until you read my next post.

    Well, more accurately, . . all will be well . . . until you reach line 44.
    Yes, you do have a line 44, and you know that for two reasons:
  • 1: When you read line 43 with nextLine(), the program doesn't block. Read the documentation link I posted in the previous post.
  • 2: Your loop is starting again, and that is where you get the exception.
  • Most text editors finish the last line with a line end sequence, which means there is a line 44, and your Scanner is going on to line 44 and finding nothing. It can cope with reading the empty line as a String, simply returning the empty String "" from nextLine(). But it can't then cope with not finding the next double, so you get a no such element exception. What is happening is that you are finding the empty line, which returns true if you ask whether there is a next line. But it won't return true if you ask whether there is anything in it. You need to find out whether there is anything in the next line, so you are looking for a next token, and there is a method which does exactly that. Suggestion to avoid the exception: stop reading when there is no more text. To do that, replace while (myScanner.hasNextLine())... with while (myScanner.hasNext())... I think deleting four letters will obviate the exception good and proper.

    Unfortunately there are all sorts of style things about your code.
     
    Carey Brown
    Saloon Keeper
    Posts: 4767
    52
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Suggestion to avoid the exception: stop reading when there is no more text. To do that, replace while (myScanner.hasNextLine())... with while (myScanner.hasNext())... I think deleting four letters will obviate the exception good and proper.

    I added line 44 prior to running this. I think I'm following your suggestion here, but it doesn't work.
     
    Campbell Ritchie
    Marshal
    Posts: 60142
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . . I think I'm following your suggestion here, . . .

    Afraid you aren't. Why is he using Strings instead of booleans in the first place?
    You are also not calling nextLine() before line 3. That will throw all your lines out of sequence, and will cause all sorts of problems. You will probably be lucky and get an exception.
     
    Carey Brown
    Saloon Keeper
    Posts: 4767
    52
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Carey Brown wrote:. . . I think I'm following your suggestion here, . . .

    Afraid you aren't. Why is he using Strings instead of booleans in the first place?
    You are also not calling nextLine() before line 3. That will throw all your lines out of sequence, and will cause all sorts of problems. You will probably be lucky and get an exception.

    Ok Cambell. I'm left to guess at your intent because your words are not  precise enough. So, this? No. It doesn't work either, whereas my post a few back did work. Can you show me?
    And the OP is generating a report so whether it is paid in part by insurance or not needs to end up being a String. Of course "true" and "false" are lousy Strings for a report, "Y" or "N" (or something similar) would be more appropriate.
     
    Campbell Ritchie
    Marshal
    Posts: 60142
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry if I wasn't clear. I shall try again.
    I have condensed the original code, and followed your suggestion of printing Y/N. Also first letter for sex: M/F

    First attempt:-Lines 26‑31 get rid of the first eight lines which are obviously the vet's name and the animal's details. Not using nextLine() in line 34 causes the remainder of the line after 7 (empty String) to go as the procedure name, and we get an input mismatch exception in line 36, probably from the first nextDouble() call. So, at risk of lots of repetition in this post, let's reinstate line 34½.Which gives us a bit more, but once we hit line 44 as I said earlier, a no such element exception. It does actually print seven records before the exception is thrown. That shows the nextLine() call all by itself (now line 35) has got all the reading back in sequence. So, let's go to while (fileScan.hasNext()) ... instead.Not only does it print seven procedures, but it even says at the end that it has read 7 records The vetDetails.txt file was made by copying and pasting OP's original code. If I condense that by removing line ends after separate tokens (not name, etc.), the output is unchanged.
     
    Carey Brown
    Saloon Keeper
    Posts: 4767
    52
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, line 31 change made all the difference. I still had it as the OP's nextLine() vs your nextInt(). Thanks for the post.
     
    Campbell Ritchie
    Marshal
    Posts: 60142
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OP: Notice new way to open the Scanner. Don't use a File object, but a Path. Read about it in the Java™ Tutorials, and the Scanner documentation.
    Don't close the Scanner. Use the strange new form of try statement, which you can read about here (also Java™ Tutorials). You will find it is called try‑with‑resources.
    Only use \r or \n if somebody has told you they want a particular sequence of line ends. Normally, use %n after printf.
     
    Campbell Ritchie
    Marshal
    Posts: 60142
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Ah, line 31 change made all the difference. . . .

    You have to count which line you are on: that will differ depending on whether you read the 7 (?dog's age) and stay on that line or move on to the next line. I have worked out how to sort that problem out for keyboard input; see here (second code block). But I have never tried that sort of thing for a file. Wold you want to write this sort of thing?Isn't the new line 33 wonderful? Won't it get OP a complete fail if he copies it?

    Thanks for the post.

    That's a pleasure
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!