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

Thread: Building Table of Specified Rows and Columns

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Building Table of Specified Rows and Columns

    Hi..there,,
    Can anyone help me with this Core servlet and javaserver program..I want to build a table consisting of rows and columns specified from the user. Below ,is the HTML page that take parameter from the user asking for how many rows and column they want..than from that it will build a table of rows and column.This is like handling a user request in term of Form Data.
    I'm having the problem on how :
    1)to convert the String input to the Integer.
    2)on the nested for loop .
    Can anyone help me..Thanks.
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    <HTML><HEAD><TITLE>Rows and Colums</TITLE></HEAD>
    <BODY BGCOLOR="#FDF5E6">
    <H1 ALIGN="CENTER">Rows and Colums Parameters</H1>
    
    <FORM ACTION="/first/homework"> 
     
      Enter no of rows:  
      <INPUT TYPE="TEXT" NAME="row"><BR>
      
      Enter no of colums: 
      <INPUT TYPE="TEXT" NAME="colums"><BR>
      
      <CENTER><INPUT TYPE="SUBMIT"></CENTER>
    
    </FORM>
    
    </BODY></HTML>

    package coreservlets;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    /* Simple servlet that reads three parameters from the
        form data.
     */
     
    public class Table extends HttpServlet {
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String title = "Reading Three Request Parameters";
        String docType =
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
          "Transitional//EN\">\n";
        out.println(docType +
                    "<HTML>\n" +
                    "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                    "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                    "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
     
                    Integer.parseInt(request.getParameter("row"));
    		Integer.parseInt(request.getParameter("colums"));
     
     
    	out.println("<table border=1 cellspacing=1 cellpadding=5>\n");
     
    	for (int i=1;i<=row ;i++)
        	{
    		out.println("<tr>");
     
             	    for (int j=1;j<=colums;j++)               
            		{ 
     
    		out.println("<td>" +  (i + j) + "</td>");
     
            		}              
     
        		out.println("<tr>");
        	}
     
    	out.println("</table>");
     
        out.println("</BODY></HTML>");
      }
    }


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

    Default Re: Building Table of Specified Rows and Columns

    To convert a string to an integer, use the Integer.parseInt function
    int nRow = Integer.parseInt(row);
    Note that if row is not an integer, a NumberFormatException will be throws, so I'd recommend validating row or surrounding the above with a try/catch block.

Similar Threads

  1. Building a Menu List
    By websey in forum AWT / Java Swing
    Replies: 1
    Last Post: November 15th, 2009, 10:34 AM
  2. Need a loop for rows and columns
    By Ceasar in forum Loops & Control Statements
    Replies: 8
    Last Post: October 9th, 2009, 05:47 PM
  3. Replies: 2
    Last Post: July 8th, 2009, 06:35 AM
  4. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM
  5. Getting table height using JSTL
    By jsnx7 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 19th, 2009, 12:03 PM

Tags for this Thread