• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 61
  • Last Modified:

I am a newbe on JAVA and have been struggling with this for hours please help : Exception in thread "main" java.util.NoSuchElementException: No line found

Here is my code:

(assume that inFS, habitat, habitatData and i are already created/initialized, as they are initialized earlier in the code)

 

case "aquarium":

           

            habitat = inFS.nextLine();

           

            while(!habitat.contains("Aquarium")) {

               

               habitat = inFS.nextLine();

            }

           

            for (i = 1; i <= 4; i++) {

             

               habitatData = habitatData + habitat + "\n";

               habitat = inFS.nextLine();

            }

           

            habitatData = habitatData.substring(4, habitatData.length());

           

            return habitatData;

 

 

The habitats.txt file it is looking at is attached.  The two other habitats, using identical code to the above (with the exception of !habitat.contains(“Aquarium”) being “Bird” or “Penguin” instead) and they both work.

 

When I hit the “aquarium” case, I get:

 



                at java.util.Scanner.nextLine(Scanner.java:1540)

                at zooprogram.FileParser.habitatInfo(FileParser.java:246)

                at zooprogram.ZooProgramMenus.habitatMenu(ZooProgramMenus.java:173)

                at zooprogram.ZooProgramMenus.mainMenuDecision(ZooProgramMenus.java:68)

                at zooprogram.ZooProgram.main(ZooProgram.java:32)

<redacted>\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 4 seconds)

 

If I change the above code to replace for(i = 1; i <= 4; i++) with for(i=1; i <= 3; i++), the code works and returns:

 

Habitat - Aquarium

Temperature: Varies with output temperature

Food source: Added daily

 

I need the last line of the Habitat – Aquarium grouping (the 4th lilne), but I can’t get it to pull it and not error ohabitats.txt
0
NAMEWITHELD12
Asked:
NAMEWITHELD12
1 Solution
 
zvitamConsultantCommented:
You should have a blank line after aquaruim you don't have so reach End of File.

I added and attched it to the post.
habitats.txt
0
 
NAMEWITHELD12Author Commented:
Awesome !!!
That fixed it!  I hit enter and saved after the final line, but what I did not do was hit enter again to produce a blank line at the end of the text box.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now