Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Servlet to generate an an m by n multiplication table based on user input  RSS feed

 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beginner question. I have a basic html form with one user input text box labeled "Enter a number" and a submit button labeled "Create a multiplication table". This is the working with a servlet to display an html formatted multiplication table. Everything else is working fine just the formatting is off; I can't seem to get the top row and far left column to number properly. It should look like this with the top row of numbers beginning with a blank cell and then numbering 1 thru n (depending on what the user selects. The first column should be the same, a blank cell and then 1 thru n. Then the multiplication table should be in the rows/columns of the table. Picture this:


I can't get mine to start with a blank cell in the top left-hand corner, instead it has 1 in that cell and the table starts calculating with 2x2.


I know having html code in a servlet is not a best practice, however this is an assignment for a course so I'm limited to what I can do without arguing with the professor. If someone could give a beginner some advice, I would appreciate the help.

Servlet code:

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You output the body of the table well. This is a nice chunk of the logic. Next try adding to this very slowly.

Right now, it outputs


Can you make it output the following. Try them out at a time. Building up slowly helps get "unstuck"
1) A first row (try making the array larger and adding a separate loop outside the nested loop)


2) A first column with all zeros in it (try making the array large, hard coding the value at index zero and adding the logic to adjust all the other indexes)


3) A first column with the right numbers in it (what variable to you have that knows what row it is in)


4) The final product (all you have to do now is add the X)



Post where you get stuck and we can help on that piece.

Also, does your professor let you use methods? Extracting all the code in the else into a method would make it easier to read.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yahoo! Patience pays off...new code and it prints the way I want it to.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Betty Christiansen wrote:Yahoo! Patience pays off...new code and it prints the way I want it to.

Congrats! I'm going to give you a cow for sticking with it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!