• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For loop problem

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 23, 2007: Message edited by: Nicholas Jordan ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ops , sorry for the alignment
 
reply
    Bookmark Topic Watch Topic
  • New Topic