Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    This is my first post. I have been working on this class for a couple of weeks now, and can't seem to get it working right. Here are the parameters:
    * Write a class named FileDisplay with the following methods:
    * 1) Constructor: Constructor should take the name of a file as an argument.
    * 2) displayHead : Method should display only the first five lines of the file.
    * 3) displayContents :Method should display the entire contents of the file.
    * 4) displayWithLineNumbers : Method should display the contents of the file. Each line should be preceded with a line number followed by a colon.

    The displayHead and displayContents methods work, however the displayWithLineNumbers method doesn't print and I get the following error:

    "Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at fileprogram.FileDisplay.displayContents(FileDispla y.java:52)
    at fileprogram.FileProgram.main(FileProgram.java:31)

    Here is my code:
     
    import java.util.Scanner;  //create scanner objects
    import java.io.*;              //used for IOException
     
    public class FileDisplay 
    {
        String fileName,    //stores file name
                  head;         //reads file for displayHead
        int count=0;         //counter
     
            //constructor to accept name of file
            public FileDisplay (String nameOfFile) 
                {
                fileName = nameOfFile;
                }
     
            //acceptor to display first 5 names in list from file
             public String displayHead() throws IOException 
                {
                //open file 
                File file = new File (fileName);
                Scanner inputHead = new Scanner(file);
     
                    //loop to count and print first 5 lines from file   
                    for (int count =1; count <=4; count++)    
                        {
                        //reads each line from file
                        head =inputHead.nextLine();
     
                        //prints each line from file
                        System.out.println(head);
                        }
     
                //returns output to main program
                return inputHead.nextLine();
                }
     
            //method to display file name and all names from file
            public String displayContents() throws IOException
                {
                //open file
                File contentFile = new File(fileName);
                Scanner inputContents = new Scanner(contentFile); 
     
                    //loop to count and print all lines from file
                    while (inputContents.hasNextLine())    
                        {
                        //reads each line from file
                        String contents = inputContents.nextLine();
     
                        //prints each line from file
                        System.out.println(contents);
                        }
     
                //returns output to main program
                return inputContents.nextLine();
                }
     
            //method to display lines from file numbered
            public String displayNumberedContents() throws IOException
                {
                //open file
                File numberedContents = new File (fileName);
                Scanner inputNumContents = new Scanner(numberedContents);
     
                    //loop to count and print lines from file and accumulate count to number lines    
                    while (inputNumContents.hasNextLine())
                        {
                        //read lines from file
                        String numContents = inputNumContents.nextLine();
     
                        //accumulator for numbering lines
                        count++;
     
                        //print numbering plus lines from file
                        System.out.print(count + numContents);                     
                        }
                //returns ouptut to main file    
                return inputNumContents.nextLine();
                } 
    }

    thanks for any feedback


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    The stack trace suggests that displayContents doesn't work either. Your return statements don't make sense at all. You're looping over the file until the last line and after the while loop you're trying to return the line after the file from your scanner object to the main method.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    So,
    Yes that may be part of my problem, the return. However in the first two methods, that return gives me the result I am looking for. So, this may be where I am going wrong. This is why I need some help with this.

    thanks

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,061
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    Suggestions:

    1. No returns are required. Get rid of them; follow the assignment's instructions.
    2. Close your Scanner objects.
    3. Add a linefeed after printing the numbered lines.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    I understand items 2 and 3, but I don't understand what you mean by "no returns are required". How do the methods get passed to the main program when called without a return?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,061
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    I think you're asking, "How does program execution resume from the point the method was called without a return?" The answer is that it happens automatically, without a return. When a method completes, program flow or execution resumes at the point the method was called. You can read more about these concepts simply by searching, 'java return from methods', and similar.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: fileDisplay class with "Exception in thread "main" java.util.NoSuchElementException: No line found" error

    Ok, I figured it out with some of your comments. Thanks to all for your input.
    Here is my final version. I am posting just for FYI. You can comment more if you feel like. Any help on improving my techniques is always welcome. Thanks again.
    import java.util.Scanner;
    import java.io.*;
     
    public class FileDisplay 
     
    {
        String fileName,    //stores file name
                head;       //reads file for displayHead
        int count,
                diffCount=0;        //counter
     
        //constructor to accept name of file
        public FileDisplay (String nameOfFile) 
        {
            fileName = nameOfFile;
        }
     
        //method to display first 5 names in list from file
        public String displayHead() throws IOException
        {
     
        //open file    
        File file = new File (fileName);
        Scanner inputHead = new Scanner(file);
     
           //loop to count and print first 5 lines from file
            for (count =1; count <=5; count++)    
           {
           //reads each line from file
           head =inputHead.nextLine();
     
           //prints each line from file
           System.out.println(head);
           }
          inputHead.close();
          //returns output to main program  
          return " "; 
     
          }
     
       //method to display file name and all names from the file
        public String displayContents() throws IOException
        {
     
        //open file
        File contentFile = new File(fileName);
        Scanner inputContents = new Scanner(contentFile); 
     
            //loop to count and print lines from file
            while (inputContents.hasNextLine())    
                {
                //reads each line from file
                String contents = inputContents.nextLine();
     
                //prints each line from file
                System.out.println(contents);
                }
     
            inputContents.close();
     
    //returns output to main program
            return "";
        }
     
        //method to display lines from file numbered
        public String displayNumberedContents() throws IOException
        {
            //open file
            File numberedContents = new File (fileName);
            Scanner inputNumContents = new Scanner(numberedContents);
     
                //loop to count and print lines from file and accumulate to number lines
                while (inputNumContents.hasNextLine())
                {
                    //read lines from file
                    String numContents = inputNumContents.nextLine();
     
                    //accumulator for numbering lines
                    diffCount++;
     
                    //print numbering plus lines from file
                    System.out.println(diffCount + " " + numContents);                     
                }
                inputNumContents.close();
     
                //returns output to main file
                return"";
        } 
    }

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 11
    Last Post: August 30th, 2012, 02:30 PM
  4. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  5. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM