Results 1 to 2 of 2
  1. #1
    charlyb-11 is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default No line found exception

    Thanks in advance for your help!

    I'm relatively new to java so forgive me if this seems like a simple thing. I'm parsing fasta files with my code. The problem is it works for some files and not others, even though all the files are formatted in exactly the same way, and I've checked in notepad++ for hidden characters. If anyone could suggest what is wrong that would be great! I can provide the files I'm parsing if needed!

    I'm getting this exception:

    run:
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at sequencecomposition.ParseFastaFile.main(ParseFasta File.java:41)
    Java Result: 1


    With this code:
    package sequencecomposition;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;

    /**
    *
    * Takes in fasta file containing sequences and splits into sequence objects
    */
    public class ParseFastaFile {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    String path = "filename";
    File file = new File(path);
    ArrayList<String> sequenceArray = new ArrayList<String>();

    try {
    Scanner scanner = new Scanner(file);

    // Runs while the scanner object has a next line
    while (scanner.hasNextLine()) {

    // Assigns the next line of the scanner object to newLine
    String newLine = scanner.nextLine();


    /** if statement to look for lines which begin with '>',
    *
    */
    if (newLine.startsWith(">")) {
    while (!(newLine = scanner.nextLine()).startsWith(">")) {
    sequenceArray.add(newLine);
    }
    /**
    * Catches the FileNotFoundException
    */
    }

    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    ProcessSequence newseq = new ProcessSequence(sequenceArray);
    newseq.measureSequenceComposition(sequenceArray);
    }
    }

    Parsing this file (extract):
    >AP012347.1 AP012347.1
    GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCAT TTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTG GAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATT CTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACTTACTA AAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATAACAATTGAAT GTCTGCACAGCCGCTTTCCACACAGACATCATAACAAAAAATTTCCACCA AACCCCCCC--


    But this file doesn't throw an exception:

    >JN580299.1 JN580299.1
    GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCAT TTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTG GAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATT CTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACTTACTA AAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATAACAATTGAAT GTCTGCACAGCCGCTTTCCACACAGACATCATAACAAAAAATTTCCACCA AACCCCCCC--

    Thanks again!

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: No line found exception

    What is going on is that your line with
    Java Code:
    while (!(newLine = scanner.nextLine()).startsWith(">")) {
    is working until it gets to the last line then, when it looks for the nextLine() it throws that error.
    If you use an if statement with another hasNextLine() call that looks like this
    Java Code:
    if (newLine.startsWith(">")) {
    				while (scanner.hasNextLine()) {
    					if(!(newLine = scanner.nextLine()).startsWith(">"))
    						sequenceArray.add(newLine);
    					else
    						break;
    				}
    It doesn't throw an error for any of them.

Similar Threads

  1. Class not found exception
    By Bhavana Jain in forum JDBC
    Replies: 6
    Last Post: 07-08-2012, 11:23 AM
  2. Replies: 0
    Last Post: 10-07-2011, 09:03 PM
  3. Class not found Exception
    By surendra in forum Java Servlet
    Replies: 8
    Last Post: 06-09-2011, 12:52 PM
  4. File not found exception on ObjectInputStream
    By pink123 in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 04:00 PM
  5. No Property Editor Found Exception -JSF 1.2
    By venkatcanwin in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-18-2010, 06:02 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
  •