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 6 of 6

Thread: Getting an ArrayIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting an ArrayIndexOutOfBoundsException

    SO this is for a homework assignment. And if you look at my previous posts you will see that I am not asking for help and I have done this work by myself. That being said I have one single error left to fix on this code, and I can not firgure out why it is giving me this error

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
     
    public class Parser {
     
    	public static int countEntries(String filename) throws IOException{
    		int numEntries = 0;
    		Scanner lineScanner = new Scanner(new File(filename));
    		while(lineScanner.hasNextLine())
    		     if (lineScanner.nextLine().startsWith("//"))
    		     	numEntries++;
    		return numEntries;
    	}
     
    	public static String[][] parse(String filename) throws IOException {
     
    		String[][] data = new String[2][countEntries(filename)];
    	    Scanner lineScanner = new Scanner(new File(filename));	
    	    boolean foundAC = false;	// True when AC line is encountered
    	    boolean foundSQ = false;	// True when SQ line is encountered
    	    String sequence="";			// accumulates the sequence
    	    String accession = "";		// stores the primary accession
    	    int n = 0;
     
    	    while(lineScanner.hasNextLine( )){
    	     	String line = lineScanner.nextLine();
    	     	String[] tokens = line.split(" ");
    	     	String s = tokens[0];
     
    	     	if (s.startsWith("//")){
    	     		data[0][n] = accession;
    	     		data[1][n] = sequence;
    	     		foundAC = foundSQ = false;
    	     		sequence = "";
    	     		n++;
    	     	}
    	     	else if (foundSQ){
    	     		sequence += line.replace(" ", "");
    	     	}
    	     	else if (s.equals("AC") && !foundAC){
    	     			foundAC = true;
    	     			accession = line.substring(5, line.length()-1).split(";")[0];
    	     	}
    	     	else if (s.equals("SQ")){
    	     		foundSQ = true;
    	     	}
    	    }
    	    lineScanner.close();
    	    return data;
    	}
     
     
    	public static void main(String[] args) throws IOException {
    		String[][] data = parse(args[0]);
    		FileWriter output = new FileWriter(new File(args[1]));
    		for (int i = 0; i < data[0].length; i++)
    			output.write(">"+data[0][i]+"\n"+data[1][i]+"\n");
    		output.close();
    	}
     
    }

    That is the entire project. It is designed to read in a file, find certain values inside the file and save thoose into an array, and then write that array into a new file. However I am getting a Indexoutofbounds error on line 56 which is this

    public static void main(String[] args) throws IOException {
    	THIS LINE HERE------------->	String[][] data = parse(args[0]);
    		FileWriter output = new FileWriter(new File(args[1]));
    		for (int i = 0; i < data[0].length; i++)
    			output.write(">"+data[0][i]+"\n"+data[1][i]+"\n");
    		output.close();


    Any and all help as to why I am getting this would be great

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting an ArrayIndexOutOfBoundsException

    Looks like you're not passing any command-line arguments to your program. What command are you using to run the program?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting an ArrayIndexOutOfBoundsException

    What do you mean? I am passing a file title "uniprotFile" into the program under the command line if that is what you mean. Could you elaborate on your question more

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting an ArrayIndexOutOfBoundsException

    If you're passing command-line args correctly then that's not the problem. Do you have the full debug stack trace?

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Getting an ArrayIndexOutOfBoundsException

    Quote Originally Posted by NewbieJavaProgrammer View Post
    ...I am passing a file title "uniprotFile" into the program...
    Try something like the following at the beginning of main():
        public static void main(String[] args) throws IOException {
            System.out.printf("Number of args = %d\n", args.length);
            System.out.printf("args[0]: %s\n", args[0]);
            String[][] data = parse(args[0]);

    It would help us understand your problem (maybe) if you can paste all output and error messages into your response. Don't abbreviate. Don't paraphrase.


    Cheers!

    Z
    Last edited by Zaphod_b; September 26th, 2012 at 03:09 PM.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting an ArrayIndexOutOfBoundsException

    I figured it out. I wasn't spelling the things right in the command prompt (because of the hard coding)

    and for those of you who helped me with the original model of this code i fixed that version as well. These codes do the same thing but written differently

    import java.util.*;
    import java.io.*;
     
    public class Parser 
    {
    	    public static void main(String[] args) throws IOException
    	    {	
    	    	PrintWriter out = new PrintWriter("fastaFile");
    	    	String[][] extractedInfo = extract_info();
    		int n = count_entries();
     
    	    	for(int i = 0; i < n; i++)
    	    	{
    			out.write("<"+extractedInfo[i][1]+"\n"+extractedInfo[i][0]+"\n");
    	    	}
     		out.close();	        
    	    }
     
     
    	    public static String[][] extract_info() throws IOException 
    	    {
    		String[][] extractedData = new String[count_entries()][2];
    	    	File uniprot = new File ("uniprotFile");
    	    	Scanner uniprotScanner = new Scanner (uniprot);
    	    	int i = 0;
    	    	boolean foundAC = false;
    		boolean SQMode = false;
    		String sequence = "";
     
    	    	while (uniprotScanner.hasNextLine())
    	    	{
    	    		String currentLine = uniprotScanner.nextLine();
    	    		String[] tokens = currentLine.split(" ");
     
    	    		if ( tokens [0].equals("AC") && !foundAC )
    	    		{
    	    			extractedData[i][1] = tokens[3];
    	    			foundAC = true;
    	    		}
    	    			else if (tokens [0].equals("SQ") && !SQMode)
    	    			{
    	    				SQMode = true;
    	    			}
    	    				else if (currentLine.startsWith("//"))
    	    				{
    	    					SQMode = foundAC = false;
    	    					extractedData[i][0] = sequence; 
    	    					sequence = "";
    	    					i++;
    	    				}	
    	    					else if (SQMode)
    	    					{
    	    						sequence += currentLine.replaceAll(" ", "");
    	    					}	    			    		
    	    	}	
    	    	return extractedData;
    	    }
     
    	    public static int count_entries() throws IOException
    	    {
    	    	int entriesCount = 0;
    	    	File uniprot = new File ("uniprotFile");
    	    	Scanner uniprotScanner = new Scanner (uniprot);
    	    while (uniprotScanner.hasNextLine())
    	    { 
    	    	String currentLine = uniprotScanner.nextLine();
    	    	String[] tokens = currentLine.split(" ");
     
    	    	if (tokens [0].equals("//"))
    	    	{
    	    		entriesCount++;
    	    	} 
    	    }
    	    return entriesCount;
    	    }
     }



    THanks for all the help. Assignment two is due monday so expect some posts here in a day or so.

    Thanks again

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException help
    By madjavapuppy in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 6th, 2012, 05:31 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By rajesh3006 in forum Member Introductions
    Replies: 1
    Last Post: March 12th, 2012, 07:32 AM
  3. Help! ArrayIndexOutOfBoundsException
    By ray3 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2011, 06:21 PM
  4. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 07:39 PM
  5. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM

Tags for this Thread