iv got a loop that checks the values in two arrays. in the event that matching values are found those values are printed to the console.
I've also included a print statement that is intended to print ONLY when no matches are found anywhere.
public class MatchTest
{
public static void main(String []args)
{
boolean matchFound=true;
int [] x = {4,5,6,1,2};
int [] y = {1,2,3};
getMatchingNumbers(x,y, matchfound);
}
//method to check numbers in each array and prints the numbers that match ( if found)
public static void getMatchingNumbers(int [] a, int []b, boolean c)
{
for(int i : a)
{
for (int j : b)
{
if (i==j)
System.out.println("matching numbers are " + i);
else
c = false;
}
}
// how do i get this to print ONLY if no matches are found any where
if (c == false)
System.out.println("no matches found");
}
}
At the moment, if the arrays passed in contain some numbers that match and some that don’t match, I’m still getting the no matches found the message. instead of this, I want the no matches found a message to print ONLY if no matches exist anywhere.
I think this should be a simple correction but I cant see where I'm going wrong.
suggestions appreciated.
c=true; break;inside the body ofif (i==j), however it is easier to have matchFound default to false and only set it to true if you find a match..