Results 1 to 9 of 9
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question how to change the layout of an input file and write to an output file

    Hi

    I have a .csv file which has a layout as schoolNo. , county1,county2,county3,county4,county5

    It will need to go into an output file as schoolNo. repeated and a county on each record .

    ie., schoolNo.,county1
    schoolNo.,county2
    schoolNo.,county3
    schoolNo.,county4
    schoolNo.,county5


    I wrote the java program as follows ..which results in this error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    BECAUSE :--
    i know bec., the first record doesnot have county3,county4,county5 . So when the if loop for county3 lenght is checked it gives this exception .

    Pls help me write this program ..thanks in advance .
    Java Code:
    /Here I open the input file and read in record by record                                                                                                                         
    		BufferedReader readin;
    		try {
    			readin = new BufferedReader(new FileReader(InFile));
    			String firstLine = readin.readLine();
    			String[] headers = firstLine.split(",");
    			columnCount = headers.length;
    			System.out.println("Columns in ZIP Master File: "+columnCount);
    			for(String input ;(input = readin.readLine()) != null; ){
    				recCount++;
    				input = input.substring(1);			
    				String[] column = input.split(",");
    									
    				if(columnCount == 10){
    					eachIP++;
    //						 OUTPUT FILE
      						if(column[1].length()> 0) {
    							String mainStr = column[0] + "," + column[1] ;  
    							Count++;
    							totalcolumns++;
    							writeOutFile.write(mainStr);
    							writeOutFile.newLine();
    						}	 
    						else{
    						bypassedCount++;
    					    }
      						if(column[2].length()>0){
      							String mainStr = column[0]+ "," + column[2];
      							Count++;
      							totalcolumns++;
    							writeOutFile.write(mainStr);
    							writeOutFile.newLine();
    						}	 
    						else{
    						bypassedCount++;
    					    }
      						if(column[3].length()>0){
      							String mainStr = column[0]+ "," + column[3];
      							Count++;
      							totalcolumns++;
    							writeOutFile.write(mainStr);
    							writeOutFile.newLine();
    						}	 
    						else{
    						bypassedCount++;
    					    }
    Last edited by renu; 05-12-2010 at 04:26 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Check the length of the column array using an if before accessing an element in the array.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Crossposted.

    kind regards,

    Jos

  4. #4
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    Hi

    I didnot understand what you want me to do in the code i wrote.

    can you pls help me , change my code .

    thank you

  5. #5
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Crossposted.

    kind regards,

    Jos
    Hi Jos

    I thought both r different websites ..and to get a reply from each website , i have posted it .
    i dont feel , i did any wrong

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by renu View Post
    Hi Jos

    I thought both r different websites ..and to get a reply from each website , i have posted it .
    i dont feel , i did any wrong
    Don't think from yourself, think about people who may spend their time answering your question only to find out that your question has also been answered on another website before; those people have wasted their time and effort. It is just courtesy to mention the fact that you have posted your question somewhere else so people can check out both threads.

    kind regards,

    Jos

  7. #7
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Hi

    The input files record looks like this
    1220,10,20,,,,

    Once the code comes to

    if(column[3].length()>0){
    String mainStr = column[0]+ "," + column[3];
    Count++;
    totalcolumns++;
    writeOutFile.write(mainStr);
    writeOutFile.newLine();
    }
    else{
    bypassedCount++;
    }



    --------------------------------------------------------------------------------
    Last edited by renu; Today at 03:26 PM.


    i get array out of bound exception..pls help
    why doesnot the array element atleast pick up null in it

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by renu View Post
    Hi

    The input files record looks like this
    1220,10,20,,,,

    Once the code comes to

    if(column[3].length()>0){
    String mainStr = column[0]+ "," + column[3];
    Count++;
    totalcolumns++;
    writeOutFile.write(mainStr);
    writeOutFile.newLine();
    }
    else{
    bypassedCount++;
    }



    --------------------------------------------------------------------------------
    i get array out of bound exception..pls help
    why doesnot the array element atleast pick up null in it
    There are two split( ... ) methods; check the other one (the one with two arguments), it allows for empty entries to be counted, i.e. split(inputString, -1) gives you the wanted results.

    kind regards,

    Jos

  9. #9
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

Similar Threads

  1. Replies: 13
    Last Post: 04-16-2010, 02:51 AM
  2. Replies: 6
    Last Post: 03-01-2010, 07:00 AM
  3. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:41 AM
  4. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  5. Replies: 1
    Last Post: 07-24-2007, 02: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
  •