7 Replies - 316 Views - Last Post: 04 February 2018 - 10:40 PM Rate Topic: -----

#1 Abhavya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-January 18

Write a Java program to make a grid of 10x10 matrices with "0"

Posted 03 February 2018 - 01:33 PM

 public class Array
{
	public static void main(String[]args)
	{
		int [][]array=new int[10][10];
		for(int row=0; row<10; row++)
		{
			for(int col=0; col<10; col++)
			{
				System.out.println(array[row][col]);
		    }
		    System.out.println();
		}
	}
}
 

My output is all vertical.I dont know where am doing it wrong.
Is This A Good Question/Topic? 0
  • +

Replies To: Write a Java program to make a grid of 10x10 matrices with "0"

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 657
  • View blog
  • Posts: 4,962
  • Joined: 25-December 13

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 03 February 2018 - 01:35 PM

Quote

My output is all vertical.I dont know where am doing it wrong.

The println() method ends its output with a newline character so that the next thing printed will go on the next line.
If you want to continue printing on the same line, use the print() method.
Was This Post Helpful? 1
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13748
  • View blog
  • Posts: 54,903
  • Joined: 12-June 08

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 03 February 2018 - 01:35 PM

There's a difference between 'print' and 'println'.. the latter being "print line" where it adds a new line after putting out the text. You need to change one of those.
Was This Post Helpful? 1
  • +
  • -

#4 Abhavya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-January 18

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 03 February 2018 - 01:45 PM

  public class Array
{
	public static void main(String[]args)
	{
		int [][]array=new int[10][10];
		for(int row=0; row<10; row++)
		{
			for(int col=0; col<10; col++)
			{
				System.out.print(array[row][col]);
		    }
		    System.out.println();
		}
	}
}



thanks a lot it's working now! :balloon:/>
Was This Post Helpful? 0
  • +
  • -

#5 Piet Souris  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 161
  • Joined: 01-December 15

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 04 February 2018 - 10:05 AM

If you want to do it without all those indices, you can make use of the fact that a 2D array in java is an array of arrays. So this is possible:
int[][] array = new int[10][10];
for (int[] a: array) System.out.println(Arrays.toString(a));

or with a java 8 stream:
Arrays.stream(array).map(Arrays::toString).forEach(System.out::println);

Was This Post Helpful? 1
  • +
  • -

#6 Abhavya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-January 18

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 04 February 2018 - 09:25 PM

We haven't done toStrings(); yet..
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 700
  • View blog
  • Posts: 2,863
  • Joined: 13-June 14

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 04 February 2018 - 10:37 PM

Personally, I'd be quite pleased if my students took the time to go and learn things that they weren't taught. Remember, at least, that a class isn't going to teach you everything about a subject.

Also, it's nice to see someone suggesting the Java 8 features!
Was This Post Helpful? 0
  • +
  • -

#8 Abhavya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-January 18

Re: Write a Java program to make a grid of 10x10 matrices with "0"

Posted 04 February 2018 - 10:40 PM

I studied how to use toString() today.But, our instructor has strictly told us to use the methods we have done in our class only.So, students won't end up getting someone else do their homework..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1