954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java help-foor loop problem :(

Hello there.i have to find if elements of a row are equal to elements of another row.this is my code but it wont work.
when k becomes 1, i want the for loop at i to start with i=2; can you help ?????
int b[][]={
{1,2,23,4,5},
{6,1,8,9,28},
{11,12,13,14,15},
{1,17,18,19,20},};
for(int k=0;k<3;i++){// when k increases with 1 i want also i to increase with 1.meaning, when k becomes 1, for loop with start with i=2;
for(int l=0;l<5;l++){
for(int i=1;i<4;i++){
for(int j=0;j<5;j++){
if(b[k][l]==b[i][j]){
System.out.print(b[k][l]+" ");
}
}
}
}
}
}
}

ndrichim
Newbie Poster
20 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 
Hello there.i have to find if elements of a row are equal to elements of another row.this is my code but it wont work. when k becomes 1, i want the for loop at i to start with i=2; can you help ????? int b[][]={ {1,2,23,4,5}, {6,1,8,9,28}, {11,12,13,14,15}, {1,17,18,19,20},}; for(int k=0;k<3;i++){// when k increases with 1 i want also i to increase with 1.meaning, when k becomes 1, for loop with start with i=2; for(int l=0;l<5;l++){ for(int i=1;i<4;i++){ for(int j=0;j<5;j++){ if(b[k][l]==b[i][j]){ System.out.print(b[k][l]+" "); } } } } } } }


why not in your for statement with 'i' do:

for(int i=1;i<4;i++){
if(k==1) i=2;
....


[EDIT] sorry how dumb of me you'll need to add this:

if(count==0) {
if(k==1) i=2;
count++;
}


and then declare the integer count as static in your main class and initialize it to 0 or else the for statement will go into a never ending loop

[EDIT] I just thought about something depending on how you want your output to look i guess you could also use:
for(int i=k+1;i<4;i++) this would produce: 1 1 1 which might be more what you want

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

thx men ;)
i will try it :)

ndrichim
Newbie Poster
20 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 
thx men ;) i will try it :)


btw ,change you for(int k= 0; k < 3;i++) to for(int k= 0; k < 3;k++) or else it will never work

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

thx.it works now :)

ndrichim
Newbie Poster
20 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: