Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default 2 Dimensional Array write error to file

    So before anyone gets all suspicious, this IS for homework. But i have done 99% of this assignment already, so any help you are willing to provide is appreciated.
    So this program has to read in a large text file of protein information, and every time it finds the letter "AC" at the beginning of the line it saves that into the two dimensional array.
    Then it is SUPPOSED to move on until it finds "SQ" at the start of the line of text, and save the following lines into the array (minus the spaces, iI'll get to that in the code)
    Then FINALLY it takes the array i just created once it is all done reading the file, and is supposed to OUTPUT the array, and save it into a text file called "fastaFile.txt"
    My questions on my code are this:
    I do not think I am storing "SQ" correctly, if i am not i would appreciate some help
    When I run the code, nothing happens. It runs without errors but doesn't do anything after that ten terminates. So I am assuming it makes the array but the output is messed up some how and is not saving to or creating the new file "fastaFile.txt" that I am trying to make
    So here is the code below, any help would be appreciated
    Thanks again

    Java Code:
    import java.util.*;
    import java.io.*;
     
    public class Perser 
    {
    	    /**
    	    * This is the main method. It takes two command-line args, the uniProtFile and fastaFile. It uses these to parse out some information.
    	    */
    	    public static void main(String[] args) throws IOException
    	    {	
    	    	PrintWriter out = new PrintWriter("fastaFile");
    	    	String[][] extractedInfo = extract_info();
     
    	    	for(int i = 0; i < extractedInfo.length; i++)
    	    	{
    	    		for(int j = 0; j < 2; j++)
    	    		{
    	    			out.println(extractedInfo[i][j]);
    	    		}
    	    		out.close();
    	    	}
     
    	        // the two file names are inside the array args
    	        // args[0] has the uniProtFile filename
    	        // args[1] has the fastaFile filename
     
    	    }
     
    	   //* public static void fileWriter() throws IOException
    	    //{
    	    //	BufferedWriter writtenFile = new BufferedWriter(new FileWriter("fastaFile.txt"));
    	    //	writtenFile.write();
    	    //}
    	   
     
     
     
    	    public static String[][] extract_info() throws IOException // note: this method does NOT have the correct return type or method parameters!
    	    {
    		    String[][] foo = new String[count_entries()][2];
    	    	File uniprot = new File ("uniprotFile");
    	    	Scanner uniprotScanner = new Scanner (uniprot);
    	    	int i = 0;
    	    	//boolean foundAC = false;
     
    	    	while (uniprotScanner.hasNextLine())
    	    	{
    	    		String currentLine = uniprotScanner.nextLine();
    	    		String[] tokens = currentLine.split(" ");
    	    	
    	    		if ( tokens [0].equals("AC"))
    	    		{
    	    			//System.out.println(tokens[3]);
    	    			tokens[3] = foo[i][1];
    	    			//foundAC = true;
    	    			
    	    		}
    	    		
    	    		else if (tokens [0].equals("SQ"))
    	    		{
    	    			String sequenceLine = uniprotScanner.nextLine();
    	    			sequenceLine.replace(' ', '\0');
    	    			//System.out.println(tokens[i]);
    	    			sequenceLine = foo[i][0];
    	    		}
    	    		else if (tokens [0].equals("//"))
    	    		{
    	    			i++;
    	    		}	    			    		
    	    	}	
    	    	return foo;
    	    }
     
    	    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;
    	    }
     }
    By the way "uniprotFile" is the file I am reading into the two dimensional array

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: 2 Dimensional Array write error to file

    Can you post what is in the array that is created: extractedInfo and add comments showing what it should be?
    and also post the contents of the input file.
    The Arrays class has a method for formatting 2D arrays for printing:
    Java Code:
         String[][] twoD = {{"dd", "ee"}, {"aa", "bb", "CC"}};
         System.out.println("twoD="+Arrays.deepToString(twoD));  // twoD=[[dd, ee], [aa, bb, CC]]

    EDIT: In the extract_info() method, where do you assign any values to elements of the foo array?
    Last edited by Norm; 09-15-2012 at 02:33 PM. Reason: spelling
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: 2 Dimensional Array write error to file

    Quote Originally Posted by NewbieJavaProgramer View Post
    i have done 99% of this assignment already
    Of course you have.
    Need help with understanding a homework assignment, not asking for answers

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Write 2d array into file
    By nikos6707 in forum New To Java
    Replies: 3
    Last Post: 03-12-2012, 06:27 AM
  2. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  3. Replies: 12
    Last Post: 02-08-2011, 02:58 PM
  4. Replies: 1
    Last Post: 02-08-2011, 09:11 AM
  5. File Write Error
    By vikain in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2008, 04:38 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
  •