Results 1 to 8 of 8
  1. #1
    MsIceCold is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default need help with this: multiplication table

    Hello everyone! I am new to java programming. I'm trying to do the exercises in the book that I am reading.

    I would to ask for a help. I am supposed to make a multiplication table. It asks a user to enter the size of the multiplication table then the output should be something like this:

    0 1 2
    1 1
    2 2 4
    3 3 6
    4 4 8
    5 5 10

    (with "1 tab" space between columns)

    Now with these codes I wrote, I am only getting 1 column. What must I do? Please help me. Thank you very much!

    Java Code:
    import java.io.*;
    
    public class MultiplicationTable {
    
        public static void main(String args[]) throws IOException {
    
            BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
    
            int num;
    
            System.out.println("Enter a number: ");
            
            num=Integer.parseInt(in.readLine());
    
            for (int x=0;x<num;x++) {
                for (int y=0;y<num;y++) {
                    System.out.println(x*y);
                }
            }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Look at the System.out.print( ... ) method versus the System.out.println( ... ) method.

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    I am only getting 1 column
    That's what you code outputs: System.out.println(x*y)
    You need to add more stuff in between the (...)s to get more columns

  4. #4
    MsIceCold is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    I've added System.out.print("\t"); before the System.out.print(x*y);

    Now I get the columns but only 1 row. lol:confused:

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    What is on that row?
    Do you know how to tell the output to go onto a new line?
    Two ways: use println() or add a "\n" (newline character) in the print() statement.

  6. #6
    MsIceCold is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    I got it already, the rows and the columns. My problem now is the output is not aligned. How can I aligned them?

    OUTPUT:

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    You need to pad the data for each column with spaces so that each column is the same width.
    I use a method which takes a string and pads it on the left with enough spaces so that the results are all the same width. For example to create columns 5 characters wide if it contained a 1 it would pad with 4 spaces on the left. If the column would contain 121 it would pad with 2 spaces on the left.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

Similar Threads

  1. Multiplication Table
    By SwEeTAcTioN in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 04:11 AM
  2. Multiplication table quiz
    By wolfy101 in forum New To Java
    Replies: 1
    Last Post: 11-16-2009, 06:29 AM
  3. Multiplication table
    By rjones215 in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 04:34 PM
  4. Replies: 2
    Last Post: 02-23-2009, 02:18 AM
  5. Help with multiplication table
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 04: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
  •