2

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.

1
  • 1
    you need a c=true; break; inside the body of if (i==j), however it is easier to have matchFound default to false and only set it to true if you find a match.. Commented Mar 2, 2018 at 12:47

4 Answers 4

3

You set c to false and only set it true if numbers match

public static void getMatchingNumbers(int [] a, int []b, boolean c){
        c = false;
        for(int i : a){
            for (int j : b){
                if (i==j){
                    System.out.println("matching numbers are " + i);
                    c = true;
                }
            }
        }
        if (!c)  System.out.println("no matches found");
    }

So you find all matches.

When you return in the if statement:

if(i==j){
    System.out.println("matching numbers are " + i);
    return;
}

you find only the first match.

Sign up to request clarification or add additional context in comments.

Comments

2

You just need to set the boolean matchFound to false at beginning. Then in the method getMatchingNumbers, add c=true to the if(i==j) block. Remove the else block as it is unneeded.

public class MatchTest {
   public static void main(String []args){
          boolean matchFound=false;//set this to FALSE at beginning
          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 a the numbers that match ( if found)
   public static void getMatchingNumbers(int [] a, int []b, boolean c){

          for(int i : a){
          for (int j : b){
//Modified the if block and removed the else block
          if (i==j){
              System.out.println("matching numbers are " + i);
              c=true;
          }

          }
   }

          if (c == false)  System.out.println("no matches found");


          }
}    

Comments

1

Do this and I'm pretty sure that your code will run without any prolem.

public static void getMatchingNumbers(int [] a, int []b, boolean c)
{
      int flag=0;
      for(int i : a)
      {
        for (int j : b)
            {
                if (i==j) 
               {
                    System.out.println("matching numbers are " + i);
                    c=false;//will turn c==false every time if a match is found
                }
            }
    }
       // change your code like this
      if (c)  
        System.out.println("no matches found");
  }       

Comments

0
public class MatchTest {



  public static void main(String[] args) {
     boolean matchFound = false;
     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 a 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);
             c = true;
         }

      }
   }
   // how do i get this to print ONLY if no matches are found any where
   if (!c) System.out.println("no matches found");


   }
}`

I made very small change in code.Your code is bad style of coding.Why you passed a boolean to function? not necessary .In Java ,that you passed boolean c into method.Actually it creates local variable boolean c inside method and copy the passed value.

2 Comments

In fairly new to java so still learning and not quite sure what you mean.... are you referring using a local Boolean in the method like this? public static void getMatchingNumbers(int [] a, int []b){ boolean c = false; for(int i : a){ for (int j : b){ if (i==j) {System.out.println("matching numbers are " + i); c = true; } } // how do i get this to print ONLY if no matches are found any where } if (!c) System.out.println("no matches found"); }
Exactly ,this is what i said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.