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: Help on 2D Multi Table Array

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

    Default Help on 2D Multi Table Array

    I need help with my code... my desired out put should be

    1 2 3
    _______
    1|1 2 3
    2|2 4 6

    can someone help me on how to add the header and the other column at the right side area... thanks!!

    import java.io.*;
     
    public class Test1 {
    	public static void print_array(int[][] num){
     
    	for (int i=1; i<num.length; i++) {
    	for (int j=1; j<num[i].length; j++)
    		System.out.print(" " +num[i][j] + " " );
    		System.out.println();
    }	
    }
    public static void main(String[] args) throws IOException {
      BufferedReader act2 = new BufferedReader ( new InputStreamReader( System.in ) );
    	String row, column;
    	int cols = 0;
    	int rows = 0;
     
    		System.out.print("Enter A Desired Number of Column(s): ");
    		column= act2.readLine();
    		cols= Integer.parseInt( column );
     
    		System.out.print("Enter A Desired Number of Row(s): ");
     		row= act2.readLine();
    		rows= Integer.parseInt( row );
     
    	int values[][] = new int[rows+1][cols+1];
    	int [][] m = new int[rows][];
     
    		for (int i=1; i < rows; i++)
    			m[i] = new int[cols];
    		for (int i=0; i < values.length; i++)
    		for (int j=0; j < values[i].length; j++)
    			values[i][j] = i*j;
     
     	System.out.println("Activity #2 Multiplication Table");
    	print_array(values);
    }
     }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Help on 2D Multi Table Array

    import java.io.*;
     
    public class Test1 {
    	public static void print_array(int[][] num){
     
    [B]	System.out.print("   ");
    	for (int i=1; i<num[0].length; i++) {
    		System.out.print(i+"  ");
    	}
     
    	System.out.println();
    	for (int i=1; i<=num[0].length; i++) {
    		System.out.print("___");
    	}
     
    	System.out.println();
    [/B]	
     
    	for (int i=1; i<num.length; i++) {
    [B]		System.out.print(""+i+"|");[/B]
    		for (int j=1; j<num[i].length; j++){
    			System.out.print(" " +num[i][j] + " " );	
    		}
    		System.out.println();
    	}	
    }
    public static void main(String[] args) throws IOException {
      BufferedReader act2 = new BufferedReader ( new InputStreamReader( System.in ) );
    	String row, column;
    	int cols = 0;
    	int rows = 0;
     
    		System.out.print("Enter A Desired Number of Column(s): ");
    		column= act2.readLine();
    		cols= Integer.parseInt( column );
     
    		System.out.print("Enter A Desired Number of Row(s): ");
     		row= act2.readLine();
    		rows= Integer.parseInt( row );
     
    	int values[][] = new int[rows+1][cols+1];
    	int [][] m = new int[rows][];
     
    		for (int i=1; i < rows; i++)
    			m[i] = new int[cols];
    		for (int i=0; i < values.length; i++)
    		for (int j=0; j < values[i].length; j++)
    			values[i][j] = i*j;
     
     	System.out.println("Activity #2 Multiplication Table");
    	print_array(values);
    }
     }
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. HTML table to 2D array parser
    By Neruk in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 28th, 2011, 07:16 AM
  2. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM
  3. Multi-dimension ArrayList example
    By adeola in forum Collections and Generics
    Replies: 3
    Last Post: March 26th, 2010, 03:23 AM
  4. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  5. Multi-dimension ArrayList example
    By helloworld922 in forum Java Programming Tutorials
    Replies: 1
    Last Post: February 3rd, 2010, 11:01 AM