Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop problem  RSS feed

 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Str

{

public static void main(String[] args)

{



String [] jjj={"venkat","venkat","venkat"};

String [] kkk={"such","such","such","such","such"};

for(int i=0; i<jjj.length;i++)

{

for(int j=0;j<i;j++)

{

System.out.println("first"+jjj[i]);

System.out.println("second"+kkk[j]);

}

}

}

}





I should get the �first� 3times and second 4 times as i go on increase the second array�

Iam getting �first� 3times and second 3 times� one less than the first
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what is your requirement.

You can debug this easier by making the contents of the string array unique from each other like venkat1, venkat2.

Can you type in the sample output that you are expecting? That will help show what your requirement is.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is your code, cleaned up a little, and placed in code tags. this preserves the formatting making it much easier to read.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the impression that the problem is in your second loop.

These are the cases where the second loop runs



if this is not what you want, evidently, there is something wrong with your condition in the second loop.
 
mamidi venkat
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All

first loop length=10;
second loop length =15;

My requirement is that Ist loop is working properly.the problem with second loop i.e second loop is working up to the first loop value 10.

iwant the second loop values to be printed up to 15 .this is my problem iam bit confused with this logic.please help me out for the proper solution for the second loop.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ September 23, 2007: Message edited by: Nicholas Jordan ]
 
chloe wong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String [] jjj={"venkat","venkat","venkat"};

String [] kkk={"such","such","such","such","such"};

for(int i=0; i<jjj.length;i++)

{

for(int j=0;j<i;j++)

{

System.out.println("first"+jjj[i]);

System.out.println("second"+kkk[j]);

}


Based on your code, there is nothing wrong with the output.

i j j<i output
----- ---- ----------- -----------
i = 0, j = 0 false -
i = 1, j = 0 true firstvenkat
i = 1, j = 1 false -
i = 2, j = 0 true firstvenkat
j = 1 true firstvenkat
j = 2 false -
i = 3

That's how it prints 3 times first "venkat" and 3 times second "such"

Not sure if this is what you want....

public class Loop {

/**
* @param args
*/

public static void main(String[] args)

{

String [] jjj={"venkat","venkat","venkat"};
String [] kkk={"such","such","such","such","such"};

for(int j = 0; j < jjj.length; j++)
{
if(j == 0)
{
System.out.println("jjj = " + jjj[j]);
}
for(int k = 0; k<kkk.length;k++)
{
if(k<jjj.length)
{
System.out.println("jjj = " + jjj[k]);
}
System.out.println("kkk = " + kkk[k]);
}

}


}

}
 
chloe wong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ops , sorry for the alignment
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!