Nested Loops

nested Loops

Page 1 of 1

1 Replies - 2463 Views - Last Post: 26 September 2006 - 02:35 PM Rate Topic: -----

#1 Rk47  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 11-October 05

Nested Loops

Posted 26 September 2006 - 01:37 PM

Hello everyone,

I wrote this program that's suppso eto print out alphabets like
aa
ab
ac

and so on .

Here is my code :

public class String {
  public static void main (String[]args){
	char [] alpha = {'a','b','c','d','e','f','g','h','i'};
	for (int i = 0; i < alpha.length; i++){
	  for (int k = 0; k < alpha.length; k++) {
		System.out.println ( alpha[k] + alpha[i]);
	  }}}}





My question is , why does it print out integers ? and offcourse what did I do wrong. Thanks a bunch.

Is This A Good Question/Topic? 0
  • +

Replies To: Nested Loops

#2 chris.tkd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06

Re: Nested Loops

Posted 26 September 2006 - 02:35 PM

Hey the reason its printing out numbers is because of the way you rote the system.out.print line. it should be

public class Test 
{
	  public static void main (String[]args)
	  {
		char [] alpha = {'a','b','c','d','e','f','g','h','i'};
		for (int i = 0; i < alpha.length; i++)
		{
		  for (int k = 0; k < alpha.length; k++)
		{
			System.out.println (alpha[i]+" "+alpha[k]);
		}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1