Results 1 to 18 of 18
  1. #1
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default need help making a multiplication table

    I am extremely new to java, and for a class project I have to create a matrix of multiplication values based on whatever number a user inputs. Numbers should be 1 through user input and the end result should look something like this:
    1 2 3
    1 1 2 3
    2 2 4 6
    3 3 6 9

    This is what I have so far, and I'm getting the output, but not in table form. Like I said I am about as new as can be to java, so any help at all would be greatly appreciated.

    Java Code:
    import javax.swing.*;
    
    public class Table
    {
    	public static void main(String args[])
    	{
    	
    		
    		String stringheader;	
    
    		int header;		
    		stringheader = JOptionPane.showInputDialog("Enter a number for your table: ");
    		//convert string to integer
    		header = Integer.parseInt(stringheader);
    		
    		//create loop
            
    
            for (int x = 0; x < header; x++) 
              {
                for (int y = 0; y < header; y++) 
                    {
                    System.out.print(x*y);
                         }}
                         
    	System.exit(0);
    	} 
    
    }
    Last edited by KAM0002; 12-07-2010 at 02:18 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I don't get any output at all

    Java Code:
    int num = 1;
    
    for (int x = 0; x > num; x++)
    {

    That code is saying "num is set to one. For x from zero and while x is bigger than num..." It won't ever execute because zero is not bigger than one (ie num) so it stops straight away. Did you really mean "is bigger than"?

    -------------------

    When you post code put [CODE] at the start of the code and [/CODE] at the end so that the code is formatted properly by the forum software.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please use CODE tags when posting code in the forum.

    Everything looks pretty good down to here:
    Java Code:
    		for (int x = 0; x > num; x++) 
    		{
    			for (int y = 0; y < num; y++) 
    			{
    				System.out.print(x*y);
    			}
    		}
    When we get to the beginning of your loop, x is set to 0. Then the rule is checked -- if x is greater than num, the loop will execute; if not, it will skip to the end of the loop. In this case, num is 1, and 0 is not greater than 1, so the loop never executes.

    -Gary-

  4. #4
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Thank you for the info, this is my very first post here I wasn't sure how to format it.

    I've changed a few things in the coding, and I'm getting the output I need but not in table form...I'm completely lost as to how to format that. Again, any help is greatly appreciated!

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Show us what your code looks like now, show us what it outputs, show us what you want it to output.

    -Gary-

  6. #6
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    The code now:
    Java Code:
    import javax.swing.*;
    
    public class Table
    {
    	public static void main(String args[])
    	{
    	
    		
    		String stringheader;	
    
    		int header;		
    		stringheader = JOptionPane.showInputDialog("Enter a number for your table: ");
    		header = Integer.parseInt(stringheader);
    	
    		
    
            for (int x = 0; x < header; x++) 
    		  {
                for (int y = 0; y < header; y++) 
    					{
                    System.out.print(x*y);
    					 }
    			}
    The output I get:
    000012024

    I need something like:

    1 2 3
    2 4 6
    3 6 9

    Basically, if the user enters the number 3, I need a table that shows the products of 1*1, 1*2, 1*3 on the first line, then 2*1, 2*2, 2*3 on the second line, etc.
    Last edited by KAM0002; 12-07-2010 at 02:32 AM.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, now start thinking about the results that were produced, and why they were produced. First of all, everything's on one line, and we don't want that, do we? How can you fix that?

    Next, you start out with four zeroes -- is that what you want?

    I bet in your trial run, you entered a 3, right? So the last number printed should be a 9, not a 4, right? What went wrong? How can you fix that?

    Fix those things, and then we can start to think about spacing. (Actually, you can start to think about it now, but it will get more complicated if numbers higher than 3 are entered -- assuming you want to keep your table looking neat.)

    -Gary-

  8. #8
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Oh my gosh, I figured it out! I made the following adjustments:
    Java Code:
     for (int x = 1; x <= header; x++) 
    		  {
                for (int y = 1; y <= header; y++) 
    					{
                    System.out.print(x*y);
    					 }
    			}
    And now I get the output:

    123246369


    But as for the spacing, I really haven't a clue. I can't find anything in my textbook about it.

    Thank you for all your help, Gary. I really appreciate it!

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    If nothing else, your textbook probably talks about a System.out.println() method in addition to the System.out.print(); method that you're already using. And you have probably seen something like System.out.print(" " + intVariable) haven't you?

    -Gary-

  10. #10
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Yes, we've talked briefly about the System.out.println();
    So now I have
    Java Code:
    for (int x = 1; x <= header; x++) 
    		  {
                for (int y = 1; y <= header; y++) 
    					{
                    System.out.print(x*y);
    					 }
    					 System.out.println();
    			}
    But I get a jumbled (when entering the number 9)
    123456789
    24681012141618
    369121518212427
    4812162024283236
    51015202530354045
    61218243036424854
    71421283542495663
    81624324048566472
    91827364554637281


    We have not talked about this that I am aware of.
    System.out.print(" " + intVariable)

  11. #11
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    I have figured it out, and am now getting the right formatting! I just added a
    Java Code:
    for (int x = 1; x <= header; x++) 
    		  {
                for (int y = 1; y <= header; y++) 
    					{
                    System.out.print((x*y) + " ");
    					 }
    					 System.out.println();
    			}

    But one last question--how would I go about having a row to the top and a row to the left with the numbers that are being multiplied? For example, if 7 were entered the top line would read 1-7 and the left column would read 1-7?

    Thank you so much again, you've been very helpful!

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, so think about what you want to do. You want a special line before the first line of multiplication results that shows all of the x values. What should that line look like? How can you print it? Then you want an extra value at the beginning of each result line, that shows the current y value. (And here's where you may discover that your nested loops are inside-out, but you had no way of noticing so far.)

    Also, one space after each result works OK as long as all of your results are one digit. What happens when some of them are two or three digits? Do you want the left sides of the numbers to line up, or their right sides? How can you make that work? Should you set a maximum on the input number you accept, so that your table doesn't overflow to the right? How can you do that?

    -Gary-

  13. #13
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    I've changed the single space to a single tab. And will my nested loops being inside out hurt the whole program if I try to add the other lines? I'm not really sure by what you mean by inside out, though...
    Last edited by KAM0002; 12-07-2010 at 03:22 AM.

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by KAM0002 View Post
    I've changed the single space to a tabbed space. And will my nested loops being inside out hurt the whole program if I try to add the other lines? I'm not really sure by what you mean by inside out, though...
    Tabs might work, but it won't be very elegant. When I say your loops are inside out -- which goes across, x or y? Which goes up and down?

    -Gary-

  15. #15
    KAM0002 is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Ohh, so switch the order of x & y? Like
    Java Code:
    for (int y = 1; y <= header; y++) 
    		  {
                for (int x = 1; x <= header; x++) 
    					{
                    System.out.print((x*y) + "	");  
    					 }
    					 System.out.println( );		
    			}

    I still am not sure what and where to make adjustments for the special row & column.

    I think it will have to be a System.out.print(something) but I don't know what that something is or where to put it.

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    That something is just spaces, but you need to know how many to use each time (and I would put the spaces before each number, not after). If it's a three-digit number, use one space, if it's a two-digit number use two spaces, and if it's a one-digit number, use three spaces. I don't think we need to worry about more than three-digit numbers, because 30*30 = 900, and I don't think you'll have more than 30 columns across.

    Also, if you do want to print a tab character, don't put an actual tab in quotes -- type \t instead.

    -Gary-

  17. #17
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Also, the special row comes before all the other rows, right? So that's where you need to put it. The special column is an extra value printed before each inner for loop. Take your time and think about it. This isn't the movies, and nobody's going to strangle Halle Berry if you don't get it done in thirty seconds. :)

    -Gary-

  18. #18
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

Similar Threads

  1. need help with this: multiplication table
    By MsIceCold in forum New To Java
    Replies: 7
    Last Post: 07-13-2010, 02:31 PM
  2. Multiplication Table
    By SwEeTAcTioN in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 05:11 AM
  3. Multiplication table quiz
    By wolfy101 in forum New To Java
    Replies: 1
    Last Post: 11-16-2009, 07:29 AM
  4. Multiplication table
    By rjones215 in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 05:34 PM
  5. Help with multiplication table
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •