Results 1 to 9 of 9
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Trying to read from a file but keep getting errors

    I have been trying to test this code which requires that I read a .txt file. But I keep getting this error:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at DNA.main(DNA.java:20)

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class DNA
    {
      public static void main (String [] args) throws IOException 
      {
       
       
       // Create Scanner object for file
        Scanner input = new Scanner (new File("dna.txt"));
         int n = input.nextInt();
        String dna1 = "";
        String dna2 = "";
        String dna3 = "";
    
       // Read Data from file
        while (input.hasNextLine())
        {
         
          dna1 = input.nextLine();
          dna2 = input.nextLine(); 	
          dna3 = input.nextLine(); 	   
          
        }
        input.close();
       System.out.println("Longest Common Sequences");
       
      //Elavuate first pair (dna1 and dna2)
       int dna1Length = dna1.length();   
       int dna2Length = dna2.length();   
    
        if( dna1Length > dna2Length)
         {
           for(int i = 0; i < dna2Length; i++)
            {
              String dna2Sub = dna2.substring(0,dna2Length - i);
    
              if( dna2Sub == dna1.substring(0,dna2Length - i) )
               {
                  System.out.println("\nPair 1: " + dna1.substring(0,dna2Length - i));
               }
              else
               {
                  System.out.println("\nPair 1: No Common Sequence Found");
               }
            }
         }
        else 
         {
          for(int i = 0; i < dna1Length; i++)
            {
              String dna1Sub = dna1.substring(0,dna1Length - i);
    
              if( dna1Sub == dna2.substring(0,dna1Length - i) )
               {
                  System.out.println("\nPair 1: " + dna2.substring(0,dna1Length - i));
               }
              else
               {
                  System.out.println("\nPair 1: No Common Sequence Found");
               }
            }
         }
        
      }
    
    
    }
    Last edited by abi; 03-22-2013 at 02:02 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Trying to read from a file but keep getting errors

    From the JDK API.

    InputMismatchException -- Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

    Perhaps the first type in the file is not an int.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Trying to read from a file but keep getting errors

    This is what the file has:
    3
    GAAGGTCGAA
    CCTCGGGA
    ATGATGGAC
    GTGATAAGGACCC
    AAATTT
    GGGCCC

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Trying to read from a file but keep getting errors

    Ok. You read in an int: Correct!!
    Then you read in three more lines: Correct!!
    Now back to reading in an int: Oops!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Trying to read from a file but keep getting errors

    I had forgotten to update the code. I placed the int variable outside the while-loop but still i get that error. I put in another thread would you mind helping me out there?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Trying to read from a file but keep getting errors

    You probably should not have done that for a variety of reasons. This particular thread has not been resolved but you started a new one on a similar topic. The moderators may not like that too much.

    Regarding your code above, you have six lines of DNA sequences but you read then in three at a time. The last three clobber the first three.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Trying to read from a file but keep getting errors

    Thanks for the info. I forgot about the advance editing options that I could have used.

    Question: How can I get it to read just 3 lines at a time?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Trying to read from a file but keep getting errors

    Why do you want it to read three lines at a time? You need to compare 2 Strings so how many lines at a time should you read?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Trying to read from a file but keep getting errors

    Expand the scope of your loop.

    Read the first three lines; process them; then continue to read the next three.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2012, 10:00 AM
  2. Replies: 0
    Last Post: 07-15-2010, 01:23 PM
  3. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  4. Netbeans errors when transferring file
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 03:41 PM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 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
  •