How to make sure an array contains certain values? [duplicate]












0
















This question already has an answer here:




  • How do I determine whether an array contains a particular value in Java?

    25 answers




I am new to coding and I am wondering how I can make sure a specific array contains certain values?



This is what I have but it is not working for me. I have tried searing the internet for a solution but I am confused by a lot of them. Any feedback helps!



public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
boolean found = new boolean[9]; // creates a boolean array

for (int row = 0; row > 1 && row > 9; row++) {
int index = arr[row] - 1;
if(!found[index]) {
found[index] = true;
} else{
return false; //returns false if there are numbers that are not between 1-9
}
}
return true;
}









share|improve this question















marked as duplicate by Hovercraft Full Of Eels java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:32






  • 2





    your for loop is wrong. try for(int row=0; row<9; row++)

    – Omurbek Kadyrbekov
    Nov 22 '18 at 3:34











  • @HovercraftFullOfEels It's tagged with sudoku.

    – shmosel
    Nov 22 '18 at 3:36






  • 1





    Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:37











  • See also Iterating through a variable length array.

    – user1803551
    Nov 22 '18 at 3:38


















0
















This question already has an answer here:




  • How do I determine whether an array contains a particular value in Java?

    25 answers




I am new to coding and I am wondering how I can make sure a specific array contains certain values?



This is what I have but it is not working for me. I have tried searing the internet for a solution but I am confused by a lot of them. Any feedback helps!



public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
boolean found = new boolean[9]; // creates a boolean array

for (int row = 0; row > 1 && row > 9; row++) {
int index = arr[row] - 1;
if(!found[index]) {
found[index] = true;
} else{
return false; //returns false if there are numbers that are not between 1-9
}
}
return true;
}









share|improve this question















marked as duplicate by Hovercraft Full Of Eels java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:32






  • 2





    your for loop is wrong. try for(int row=0; row<9; row++)

    – Omurbek Kadyrbekov
    Nov 22 '18 at 3:34











  • @HovercraftFullOfEels It's tagged with sudoku.

    – shmosel
    Nov 22 '18 at 3:36






  • 1





    Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:37











  • See also Iterating through a variable length array.

    – user1803551
    Nov 22 '18 at 3:38
















0












0








0









This question already has an answer here:




  • How do I determine whether an array contains a particular value in Java?

    25 answers




I am new to coding and I am wondering how I can make sure a specific array contains certain values?



This is what I have but it is not working for me. I have tried searing the internet for a solution but I am confused by a lot of them. Any feedback helps!



public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
boolean found = new boolean[9]; // creates a boolean array

for (int row = 0; row > 1 && row > 9; row++) {
int index = arr[row] - 1;
if(!found[index]) {
found[index] = true;
} else{
return false; //returns false if there are numbers that are not between 1-9
}
}
return true;
}









share|improve this question

















This question already has an answer here:




  • How do I determine whether an array contains a particular value in Java?

    25 answers




I am new to coding and I am wondering how I can make sure a specific array contains certain values?



This is what I have but it is not working for me. I have tried searing the internet for a solution but I am confused by a lot of them. Any feedback helps!



public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
boolean found = new boolean[9]; // creates a boolean array

for (int row = 0; row > 1 && row > 9; row++) {
int index = arr[row] - 1;
if(!found[index]) {
found[index] = true;
} else{
return false; //returns false if there are numbers that are not between 1-9
}
}
return true;
}




This question already has an answer here:




  • How do I determine whether an array contains a particular value in Java?

    25 answers








java arrays sudoku






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 3:34









Hovercraft Full Of Eels

262k20213319




262k20213319










asked Nov 22 '18 at 3:29









SaraSara

11




11




marked as duplicate by Hovercraft Full Of Eels java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Hovercraft Full Of Eels java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:32






  • 2





    your for loop is wrong. try for(int row=0; row<9; row++)

    – Omurbek Kadyrbekov
    Nov 22 '18 at 3:34











  • @HovercraftFullOfEels It's tagged with sudoku.

    – shmosel
    Nov 22 '18 at 3:36






  • 1





    Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:37











  • See also Iterating through a variable length array.

    – user1803551
    Nov 22 '18 at 3:38
















  • 3





    This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:32






  • 2





    your for loop is wrong. try for(int row=0; row<9; row++)

    – Omurbek Kadyrbekov
    Nov 22 '18 at 3:34











  • @HovercraftFullOfEels It's tagged with sudoku.

    – shmosel
    Nov 22 '18 at 3:36






  • 1





    Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 3:37











  • See also Iterating through a variable length array.

    – user1803551
    Nov 22 '18 at 3:38










3




3





This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

– Hovercraft Full Of Eels
Nov 22 '18 at 3:32





This isn't for a Sudoku type of program, is it? Also, keep fluff out of your question, such as "I have tried searching.... but I am confused...". Instead show specifically what you've found and state specifically how it confuses you. Try to include information that helps clarify and specify your question.

– Hovercraft Full Of Eels
Nov 22 '18 at 3:32




2




2





your for loop is wrong. try for(int row=0; row<9; row++)

– Omurbek Kadyrbekov
Nov 22 '18 at 3:34





your for loop is wrong. try for(int row=0; row<9; row++)

– Omurbek Kadyrbekov
Nov 22 '18 at 3:34













@HovercraftFullOfEels It's tagged with sudoku.

– shmosel
Nov 22 '18 at 3:36





@HovercraftFullOfEels It's tagged with sudoku.

– shmosel
Nov 22 '18 at 3:36




1




1





Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

– Hovercraft Full Of Eels
Nov 22 '18 at 3:37





Ah thanks @shmosel, didn't see that. Original poster, then you really aren't using numbers at all, but 9 distinct symbols. Use an enum instead of ints.

– Hovercraft Full Of Eels
Nov 22 '18 at 3:37













See also Iterating through a variable length array.

– user1803551
Nov 22 '18 at 3:38







See also Iterating through a variable length array.

– user1803551
Nov 22 '18 at 3:38














2 Answers
2






active

oldest

votes


















0














/**
* Checks if this array consists of numbers 1 to 9, with all unique numbers,
* and no number missing.
*
* @param arr input array
* @return true if this array has numbers 1 to 9, each occurring exactly once
*/
public static boolean allnumbers(int arr) {
if (arr.length != 9) return false;
return IntStream.rangeClosed(1, 9)
.noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
}


Another way...



public static boolean allnumbers(int arr) {
return Arrays.stream(arr).boxed().collect(Collectors.toSet())
.equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
}


Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-



public static boolean allnumbers(int arr) {
return Arrays.stream(arr)
.noneMatch(i -> i < 1 || i > 9);
}





share|improve this answer

































    -1














    This is a solution for your problem:



    public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
    for(int row=0; row < 9; row++){
    if (arr[row] < 1 || arr[row] > 9)
    return false;
    }
    return true;
    }


    this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      /**
      * Checks if this array consists of numbers 1 to 9, with all unique numbers,
      * and no number missing.
      *
      * @param arr input array
      * @return true if this array has numbers 1 to 9, each occurring exactly once
      */
      public static boolean allnumbers(int arr) {
      if (arr.length != 9) return false;
      return IntStream.rangeClosed(1, 9)
      .noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
      }


      Another way...



      public static boolean allnumbers(int arr) {
      return Arrays.stream(arr).boxed().collect(Collectors.toSet())
      .equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
      }


      Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-



      public static boolean allnumbers(int arr) {
      return Arrays.stream(arr)
      .noneMatch(i -> i < 1 || i > 9);
      }





      share|improve this answer






























        0














        /**
        * Checks if this array consists of numbers 1 to 9, with all unique numbers,
        * and no number missing.
        *
        * @param arr input array
        * @return true if this array has numbers 1 to 9, each occurring exactly once
        */
        public static boolean allnumbers(int arr) {
        if (arr.length != 9) return false;
        return IntStream.rangeClosed(1, 9)
        .noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
        }


        Another way...



        public static boolean allnumbers(int arr) {
        return Arrays.stream(arr).boxed().collect(Collectors.toSet())
        .equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
        }


        Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-



        public static boolean allnumbers(int arr) {
        return Arrays.stream(arr)
        .noneMatch(i -> i < 1 || i > 9);
        }





        share|improve this answer




























          0












          0








          0







          /**
          * Checks if this array consists of numbers 1 to 9, with all unique numbers,
          * and no number missing.
          *
          * @param arr input array
          * @return true if this array has numbers 1 to 9, each occurring exactly once
          */
          public static boolean allnumbers(int arr) {
          if (arr.length != 9) return false;
          return IntStream.rangeClosed(1, 9)
          .noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
          }


          Another way...



          public static boolean allnumbers(int arr) {
          return Arrays.stream(arr).boxed().collect(Collectors.toSet())
          .equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
          }


          Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-



          public static boolean allnumbers(int arr) {
          return Arrays.stream(arr)
          .noneMatch(i -> i < 1 || i > 9);
          }





          share|improve this answer















          /**
          * Checks if this array consists of numbers 1 to 9, with all unique numbers,
          * and no number missing.
          *
          * @param arr input array
          * @return true if this array has numbers 1 to 9, each occurring exactly once
          */
          public static boolean allnumbers(int arr) {
          if (arr.length != 9) return false;
          return IntStream.rangeClosed(1, 9)
          .noneMatch(value -> Arrays.stream(arr).noneMatch(a -> a == value));
          }


          Another way...



          public static boolean allnumbers(int arr) {
          return Arrays.stream(arr).boxed().collect(Collectors.toSet())
          .equals(IntStream.rangeClosed(1, 9).boxed().collect(Collectors.toSet()));
          }


          Or if you want to check only the fact that no number is outside the range of 1-9, you can use this:-



          public static boolean allnumbers(int arr) {
          return Arrays.stream(arr)
          .noneMatch(i -> i < 1 || i > 9);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 4:14

























          answered Nov 22 '18 at 3:38









          KartikKartik

          4,46731537




          4,46731537

























              -1














              This is a solution for your problem:



              public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
              for(int row=0; row < 9; row++){
              if (arr[row] < 1 || arr[row] > 9)
              return false;
              }
              return true;
              }


              this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.






              share|improve this answer




























                -1














                This is a solution for your problem:



                public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
                for(int row=0; row < 9; row++){
                if (arr[row] < 1 || arr[row] > 9)
                return false;
                }
                return true;
                }


                this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.






                share|improve this answer


























                  -1












                  -1








                  -1







                  This is a solution for your problem:



                  public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
                  for(int row=0; row < 9; row++){
                  if (arr[row] < 1 || arr[row] > 9)
                  return false;
                  }
                  return true;
                  }


                  this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.






                  share|improve this answer













                  This is a solution for your problem:



                  public static boolean allnumbers(int arr) {// makes sure only numbers 1-9 are used
                  for(int row=0; row < 9; row++){
                  if (arr[row] < 1 || arr[row] > 9)
                  return false;
                  }
                  return true;
                  }


                  this function will return true if and only if and only if the first 9 elements in arr are between 1 and 9 inclusive.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 3:36









                  PhaseRushPhaseRush

                  22618




                  22618















                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?