简体   繁体   中英

How to print a statement from a for loop only once - Java

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.

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.

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");


          }
}    
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.

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");
  }       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM