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

Thread: I want to improve my code.

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

    Question I want to improve my code.

    Hello, I wrote a program to extract table data from HTML document.
    However I want to shrink this code more because it has to use large array variables.
    Could you give me any advice for improving this code more?

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    // This program copies all of table data 
    //   in a HTML document into the array of String type.
    class Tblextrct {
            enum STATE {cnct, dntct};
     
        	public Tblextrct(){		
            }
     
        	public String [][] tableAnalysis(String ss, boolean debug){
     
                Pattern trs  = Pattern.compile("^<tr");
                Matcher trsm;
                Pattern tre  = Pattern.compile("^</tr>");
                Matcher trem;
                Pattern tds  = Pattern.compile("^<td");
                Matcher tdsm;
                Pattern tde  = Pattern.compile("^</td>");
                Matcher tdem;
                Pattern tags = Pattern.compile("^<(table|/table|tr|/tr|td|/td)");
                Matcher tagm ;
                Pattern ntag = Pattern.compile("^[^<]");
                Matcher ntagm ;
                Pattern spc = Pattern.compile("^(\\s+)$");
                Matcher spcm ;
     
                int i = 0, j = 0, x = 100*1000, y = 50, xm = 0, ym = 0 ;
        		String [][] array = new String [x][y];
                String []   stk   = new String[x]; 
                String []   stk1  = new String[x];
                String      cc    = "", dd = "";            
     
                ss = ss.replace("<b>", "");
                ss = ss.replace("</b>", "");
     
                for ( i = 0 ; i < ss.length();i++){
                	cc = ss.substring(i,i+1);
                	if ( cc.equals("<")){
                	   stk[j] = dd;
                       j++;
                	   dd = cc;
                	} else if ( cc.equals(">")){
                		stk[j] = dd + cc;
                		j++;
                		dd = "";
                	} else {
                		dd = dd + cc;
                	}        	
                }
     
                j = 0 ;
                for ( i = 0 ; i < stk.length ; i++){
                	if ( stk[i] != null ){
                		tagm = tags.matcher(stk[i]);
                		ntagm = ntag.matcher(stk[i]);
                		if ( (ntagm.find() || tagm.find()) && !stk[i].equals("") ){
                		   stk1[j]=stk[i];
                		   j++;
                	   }
                	}
                }
     
                j = 0 ; x = 0; y = 0;
                for ( i = 0 ; i < stk1.length ; i++){
                	if ( stk1[i] != null ){
                    	trsm  = trs.matcher(stk1[i]);
                    	trem  = tre.matcher(stk1[i]);
                    	tdsm  = tds.matcher(stk1[i]);
                    	tdem  = tde.matcher(stk1[i]);
                    	ntagm = ntag.matcher(stk1[i]);
                    	spcm  = spc.matcher(stk1[i]);
     
                    	if ( ntagm.find() && !spcm.find() ){
              	    	  if (array[x][y] != null){
              	    	     array[x][y]= array[x][y] + " " + stk1[i];
              	    	     if (debug){
                 	    	  System.out.println(">0 : "+x+","+y+" "+array[x][y]);
              	    	     }
              	    	  } else {
              	    		 array[x][y] = stk1[i];
              	    		 if (debug){
                	    	  System.out.println("<0 : "+x+","+y+" "+stk1[i]);
              	    		 }
              	    	  }
              	       } else if ( trsm.find() ){
              	    	  x++;
              	          y = 0;
              	       } else if ( trem.find()){
              		      if ( xm < x ){
              		    	  xm = x;
              		      }
              	       } else if ( tdsm.find()){
              		      y++;
              	       } else if ( tdem.find()){
              		      if ( ym < y ){
              		    	  ym = y;
              		      }
              	       }
                	}
                }
     
                xm++;
                ym++;
                if (debug){
                   System.out.println("xm="+xm);
                   System.out.println("ym="+ym);
                }
                String [][] ary = new String [xm][ym];
                for ( i = 0 ; i < xm ; i++){
                	for ( j = 0 ; j < ym ; j++ ){
                		if ( array[i][j] == null ){
                		   ary[i][j] = "";
                		}else{
                		   ary[i][j] = array[i][j];
                		}
                	}
                }
     
        		return ary;
        	}
        }

    class Test { 
        public static void main(String args[]) {
           int    x = 0 , y = 0;
           String [][] ss ;
           String sa = "";
           Tblextrct tt = new Tblextrct();
     
           // a input HTML description.
           String tb ="<table border=\"5\" cellspacing=\"15\" cellpadding=\"10\">\n<tr> <td>Apple</td>    <td>Sweet-sour</td>    <td>Not quite red</td>  </tr>  <tr>    <td>Chinese citron</td>    <td>Quite sour</td>    <td>Almost yellow</td>  </tr></table>";
     
           ss = tt.tableAnalysis(tb, false);
     
           for ( x = 0 ; x < (ss.length);x++){
        	   sa = "";
        	   for ( y = 0 ; y < (ss[x].length); y++){
        		   if ( ss[x][y] != null){
        		     sa = sa + "," + ss[x][y];
        		   } 
        	   }
               System.out.println("ss["+x+"]={"+sa+"}");
           }
        }  
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I want to improve my code.

    If it ain't broken, don't fix it
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I want to improve my code.

    One suggestion would be to use variable names with some meaning.
    array does not say anything about what it contains or how it is used and you can tell it is an array by the syntax.

    Also Using program Comments helps people understand what the code is supposed to do.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I want to improve my code.

    Like Newbie said, if it ain't broke don't try and fix it. That being said, your code makes very little sense given the obfuscated variable names and lack of comments that Norm pointed out. To add to their advice, I fail to see why you using regular expressions without harnessing the power of them...you could have a single regex that pulls the text inside a table element, then other regex that pulls the nested tags (tr, td...). Using a design such as this, your method could probably be shortened 10 fold, and a lot more understandable, and if written properly can be used to pull out a lot more than just a table.

Similar Threads

  1. Help me improve my code.
    By JeremiahWalker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 10th, 2012, 09:24 PM
  2. How can i improve this code?
    By ziplague in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 12th, 2011, 12:24 PM
  3. Best book to learn/improve at Java
    By Melawe in forum The Cafe
    Replies: 1
    Last Post: December 5th, 2011, 08:28 AM
  4. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  5. Critique Java Game: Help Me Improve
    By gretty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 21st, 2010, 07:49 PM