Want to use an array created in a method, for further use in another method











up vote
0
down vote

favorite












I have a problem and the title actually sums it up perfectly. So i'll just go ahead and show you the code snippet.
So the methode generate, is generating an array, that is filled with numbers between 1 and 1000, including both. The length of the array is user input.
The next method, isPrime, is gonna conclude if its a prime number, so i can use those numbers with the true condition in another method. The generate method works but in isPrime i always get errors. If u can think of a better way, let me know please.



static int generate(int n) {
int arr = new int[n+1];
for(int x = 0; x <= n; x ++) {
int number = (int) (Math.random()* 999)+1;
arr[x] = number;
}
return arr;
}

static int isPrime(int p, final int q) {
boolean itIs = true;
//final int arr;

for(int r = 0; r <= p; r++) { // here it somehow states r is deadCode
for(int j = 2; j < q[r]; j++) {
if(q[r]%j == 0) {
itIs = false;
}
}
return q[r];
}

}









share|improve this question






















  • Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
    – GBlodgett
    Nov 20 at 1:26












  • Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
    – cripplingdepression
    Nov 20 at 1:27












  • When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
    – GBlodgett
    Nov 20 at 1:29










  • Ah alright that is indeed a good question. I will try it out with a boolean return.
    – cripplingdepression
    Nov 20 at 1:30










  • Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
    – GBlodgett
    Nov 20 at 1:32















up vote
0
down vote

favorite












I have a problem and the title actually sums it up perfectly. So i'll just go ahead and show you the code snippet.
So the methode generate, is generating an array, that is filled with numbers between 1 and 1000, including both. The length of the array is user input.
The next method, isPrime, is gonna conclude if its a prime number, so i can use those numbers with the true condition in another method. The generate method works but in isPrime i always get errors. If u can think of a better way, let me know please.



static int generate(int n) {
int arr = new int[n+1];
for(int x = 0; x <= n; x ++) {
int number = (int) (Math.random()* 999)+1;
arr[x] = number;
}
return arr;
}

static int isPrime(int p, final int q) {
boolean itIs = true;
//final int arr;

for(int r = 0; r <= p; r++) { // here it somehow states r is deadCode
for(int j = 2; j < q[r]; j++) {
if(q[r]%j == 0) {
itIs = false;
}
}
return q[r];
}

}









share|improve this question






















  • Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
    – GBlodgett
    Nov 20 at 1:26












  • Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
    – cripplingdepression
    Nov 20 at 1:27












  • When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
    – GBlodgett
    Nov 20 at 1:29










  • Ah alright that is indeed a good question. I will try it out with a boolean return.
    – cripplingdepression
    Nov 20 at 1:30










  • Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
    – GBlodgett
    Nov 20 at 1:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a problem and the title actually sums it up perfectly. So i'll just go ahead and show you the code snippet.
So the methode generate, is generating an array, that is filled with numbers between 1 and 1000, including both. The length of the array is user input.
The next method, isPrime, is gonna conclude if its a prime number, so i can use those numbers with the true condition in another method. The generate method works but in isPrime i always get errors. If u can think of a better way, let me know please.



static int generate(int n) {
int arr = new int[n+1];
for(int x = 0; x <= n; x ++) {
int number = (int) (Math.random()* 999)+1;
arr[x] = number;
}
return arr;
}

static int isPrime(int p, final int q) {
boolean itIs = true;
//final int arr;

for(int r = 0; r <= p; r++) { // here it somehow states r is deadCode
for(int j = 2; j < q[r]; j++) {
if(q[r]%j == 0) {
itIs = false;
}
}
return q[r];
}

}









share|improve this question













I have a problem and the title actually sums it up perfectly. So i'll just go ahead and show you the code snippet.
So the methode generate, is generating an array, that is filled with numbers between 1 and 1000, including both. The length of the array is user input.
The next method, isPrime, is gonna conclude if its a prime number, so i can use those numbers with the true condition in another method. The generate method works but in isPrime i always get errors. If u can think of a better way, let me know please.



static int generate(int n) {
int arr = new int[n+1];
for(int x = 0; x <= n; x ++) {
int number = (int) (Math.random()* 999)+1;
arr[x] = number;
}
return arr;
}

static int isPrime(int p, final int q) {
boolean itIs = true;
//final int arr;

for(int r = 0; r <= p; r++) { // here it somehow states r is deadCode
for(int j = 2; j < q[r]; j++) {
if(q[r]%j == 0) {
itIs = false;
}
}
return q[r];
}

}






java methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 1:10









cripplingdepression

1




1












  • Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
    – GBlodgett
    Nov 20 at 1:26












  • Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
    – cripplingdepression
    Nov 20 at 1:27












  • When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
    – GBlodgett
    Nov 20 at 1:29










  • Ah alright that is indeed a good question. I will try it out with a boolean return.
    – cripplingdepression
    Nov 20 at 1:30










  • Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
    – GBlodgett
    Nov 20 at 1:32


















  • Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
    – GBlodgett
    Nov 20 at 1:26












  • Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
    – cripplingdepression
    Nov 20 at 1:27












  • When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
    – GBlodgett
    Nov 20 at 1:29










  • Ah alright that is indeed a good question. I will try it out with a boolean return.
    – cripplingdepression
    Nov 20 at 1:30










  • Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
    – GBlodgett
    Nov 20 at 1:32
















Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
– GBlodgett
Nov 20 at 1:26






Probably because you return after the first iteration of the outer for loop. Also if this is an isPrime() method why are you returning an int? And why are you returning the element of the array you are testing?
– GBlodgett
Nov 20 at 1:26














Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
– cripplingdepression
Nov 20 at 1:27






Yeah i tried it already outside and it states that r isn't resolved to a variable. EDIT: When the itIs boolean is true, i want to make another method, which creates another array that takes the true array numbers.
– cripplingdepression
Nov 20 at 1:27














When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
– GBlodgett
Nov 20 at 1:29




When I plug this into my IDE it says nothing about dead code. It says that you are missing a return statement. But again I ask why are you return q[r] and not a boolean result?
– GBlodgett
Nov 20 at 1:29












Ah alright that is indeed a good question. I will try it out with a boolean return.
– cripplingdepression
Nov 20 at 1:30




Ah alright that is indeed a good question. I will try it out with a boolean return.
– cripplingdepression
Nov 20 at 1:30












Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
– GBlodgett
Nov 20 at 1:32




Also I would recommend only have isPrime accept a single int, and not an entire Array. Also you will find that you will need to utilize a break or return statement if itIs ever becomes false so the result will not get overridden
– GBlodgett
Nov 20 at 1:32












2 Answers
2






active

oldest

votes

















up vote
0
down vote













First, create a method to check a value is prime:



public boolean isPrime(int value) {
for (int i = 0; i < value / 2; i++) { // value / 2 is enough, doesn't need to check all values
if (value % i == 0) {
return false;
}
}
return true;
}


Then you check each value of array and put prime value to new array:



public int filterArray(int array) {
List<Integer> intList = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (isPrime(array[i])) {
intList.add(array[i]);
}
}
Integer integerArray = intList.toArray(new Integer[intList.size()]);
int intArray = ArrayUtils.toPrimitive(integerArray);
return intArray;
}


Then you get the filtered prime array.






share|improve this answer




























    up vote
    0
    down vote













    It still doesn't work but i changed it to this.



    static int generate(int n) {
    int arr = new int[n+1];
    for(int x = 0; x <= n; x ++) {
    int number = (int) (Math.random()* 999)+1;
    arr[x] = number;
    }
    return arr;
    }


    static boolean isPrime(int p) {
    boolean itIs = true;
    for(int j = 2; j < p; j++) {
    if(p%j == 0) {
    itIs = false;
    }
    }
    System.out.print(p + "bla" + itIs);
    return itIs;
    }

    //numberCount, isPrime, generate()
    static void filterPrimeNumbers(int zahlmenge,boolean t, final int u) {
    int arr2 = new int[0];
    int l = 0;
    for(int n = 0; n <= zahlmenge; n++) {
    if(t) {
    arr2[l] = u[n];
    l++;
    }
    System.out.println(arr2[0]);
    }

    }

    public static void main(String args) {

    System.out.print("Give me a number: ");
    Scanner sc = new Scanner(System.in);
    int numberCount = sc.nextInt();
    filterPrimeNumbers(numberCount, isPrime(generate(numberCount)[0]), generate(numberCount));

    }


    }






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384836%2fwant-to-use-an-array-created-in-a-method-for-further-use-in-another-method%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      First, create a method to check a value is prime:



      public boolean isPrime(int value) {
      for (int i = 0; i < value / 2; i++) { // value / 2 is enough, doesn't need to check all values
      if (value % i == 0) {
      return false;
      }
      }
      return true;
      }


      Then you check each value of array and put prime value to new array:



      public int filterArray(int array) {
      List<Integer> intList = new ArrayList<>();
      for (int i = 0; i < array.length; i++) {
      if (isPrime(array[i])) {
      intList.add(array[i]);
      }
      }
      Integer integerArray = intList.toArray(new Integer[intList.size()]);
      int intArray = ArrayUtils.toPrimitive(integerArray);
      return intArray;
      }


      Then you get the filtered prime array.






      share|improve this answer

























        up vote
        0
        down vote













        First, create a method to check a value is prime:



        public boolean isPrime(int value) {
        for (int i = 0; i < value / 2; i++) { // value / 2 is enough, doesn't need to check all values
        if (value % i == 0) {
        return false;
        }
        }
        return true;
        }


        Then you check each value of array and put prime value to new array:



        public int filterArray(int array) {
        List<Integer> intList = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
        if (isPrime(array[i])) {
        intList.add(array[i]);
        }
        }
        Integer integerArray = intList.toArray(new Integer[intList.size()]);
        int intArray = ArrayUtils.toPrimitive(integerArray);
        return intArray;
        }


        Then you get the filtered prime array.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          First, create a method to check a value is prime:



          public boolean isPrime(int value) {
          for (int i = 0; i < value / 2; i++) { // value / 2 is enough, doesn't need to check all values
          if (value % i == 0) {
          return false;
          }
          }
          return true;
          }


          Then you check each value of array and put prime value to new array:



          public int filterArray(int array) {
          List<Integer> intList = new ArrayList<>();
          for (int i = 0; i < array.length; i++) {
          if (isPrime(array[i])) {
          intList.add(array[i]);
          }
          }
          Integer integerArray = intList.toArray(new Integer[intList.size()]);
          int intArray = ArrayUtils.toPrimitive(integerArray);
          return intArray;
          }


          Then you get the filtered prime array.






          share|improve this answer












          First, create a method to check a value is prime:



          public boolean isPrime(int value) {
          for (int i = 0; i < value / 2; i++) { // value / 2 is enough, doesn't need to check all values
          if (value % i == 0) {
          return false;
          }
          }
          return true;
          }


          Then you check each value of array and put prime value to new array:



          public int filterArray(int array) {
          List<Integer> intList = new ArrayList<>();
          for (int i = 0; i < array.length; i++) {
          if (isPrime(array[i])) {
          intList.add(array[i]);
          }
          }
          Integer integerArray = intList.toArray(new Integer[intList.size()]);
          int intArray = ArrayUtils.toPrimitive(integerArray);
          return intArray;
          }


          Then you get the filtered prime array.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 2:40









          Bejond

          872515




          872515
























              up vote
              0
              down vote













              It still doesn't work but i changed it to this.



              static int generate(int n) {
              int arr = new int[n+1];
              for(int x = 0; x <= n; x ++) {
              int number = (int) (Math.random()* 999)+1;
              arr[x] = number;
              }
              return arr;
              }


              static boolean isPrime(int p) {
              boolean itIs = true;
              for(int j = 2; j < p; j++) {
              if(p%j == 0) {
              itIs = false;
              }
              }
              System.out.print(p + "bla" + itIs);
              return itIs;
              }

              //numberCount, isPrime, generate()
              static void filterPrimeNumbers(int zahlmenge,boolean t, final int u) {
              int arr2 = new int[0];
              int l = 0;
              for(int n = 0; n <= zahlmenge; n++) {
              if(t) {
              arr2[l] = u[n];
              l++;
              }
              System.out.println(arr2[0]);
              }

              }

              public static void main(String args) {

              System.out.print("Give me a number: ");
              Scanner sc = new Scanner(System.in);
              int numberCount = sc.nextInt();
              filterPrimeNumbers(numberCount, isPrime(generate(numberCount)[0]), generate(numberCount));

              }


              }






              share|improve this answer

























                up vote
                0
                down vote













                It still doesn't work but i changed it to this.



                static int generate(int n) {
                int arr = new int[n+1];
                for(int x = 0; x <= n; x ++) {
                int number = (int) (Math.random()* 999)+1;
                arr[x] = number;
                }
                return arr;
                }


                static boolean isPrime(int p) {
                boolean itIs = true;
                for(int j = 2; j < p; j++) {
                if(p%j == 0) {
                itIs = false;
                }
                }
                System.out.print(p + "bla" + itIs);
                return itIs;
                }

                //numberCount, isPrime, generate()
                static void filterPrimeNumbers(int zahlmenge,boolean t, final int u) {
                int arr2 = new int[0];
                int l = 0;
                for(int n = 0; n <= zahlmenge; n++) {
                if(t) {
                arr2[l] = u[n];
                l++;
                }
                System.out.println(arr2[0]);
                }

                }

                public static void main(String args) {

                System.out.print("Give me a number: ");
                Scanner sc = new Scanner(System.in);
                int numberCount = sc.nextInt();
                filterPrimeNumbers(numberCount, isPrime(generate(numberCount)[0]), generate(numberCount));

                }


                }






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  It still doesn't work but i changed it to this.



                  static int generate(int n) {
                  int arr = new int[n+1];
                  for(int x = 0; x <= n; x ++) {
                  int number = (int) (Math.random()* 999)+1;
                  arr[x] = number;
                  }
                  return arr;
                  }


                  static boolean isPrime(int p) {
                  boolean itIs = true;
                  for(int j = 2; j < p; j++) {
                  if(p%j == 0) {
                  itIs = false;
                  }
                  }
                  System.out.print(p + "bla" + itIs);
                  return itIs;
                  }

                  //numberCount, isPrime, generate()
                  static void filterPrimeNumbers(int zahlmenge,boolean t, final int u) {
                  int arr2 = new int[0];
                  int l = 0;
                  for(int n = 0; n <= zahlmenge; n++) {
                  if(t) {
                  arr2[l] = u[n];
                  l++;
                  }
                  System.out.println(arr2[0]);
                  }

                  }

                  public static void main(String args) {

                  System.out.print("Give me a number: ");
                  Scanner sc = new Scanner(System.in);
                  int numberCount = sc.nextInt();
                  filterPrimeNumbers(numberCount, isPrime(generate(numberCount)[0]), generate(numberCount));

                  }


                  }






                  share|improve this answer












                  It still doesn't work but i changed it to this.



                  static int generate(int n) {
                  int arr = new int[n+1];
                  for(int x = 0; x <= n; x ++) {
                  int number = (int) (Math.random()* 999)+1;
                  arr[x] = number;
                  }
                  return arr;
                  }


                  static boolean isPrime(int p) {
                  boolean itIs = true;
                  for(int j = 2; j < p; j++) {
                  if(p%j == 0) {
                  itIs = false;
                  }
                  }
                  System.out.print(p + "bla" + itIs);
                  return itIs;
                  }

                  //numberCount, isPrime, generate()
                  static void filterPrimeNumbers(int zahlmenge,boolean t, final int u) {
                  int arr2 = new int[0];
                  int l = 0;
                  for(int n = 0; n <= zahlmenge; n++) {
                  if(t) {
                  arr2[l] = u[n];
                  l++;
                  }
                  System.out.println(arr2[0]);
                  }

                  }

                  public static void main(String args) {

                  System.out.print("Give me a number: ");
                  Scanner sc = new Scanner(System.in);
                  int numberCount = sc.nextInt();
                  filterPrimeNumbers(numberCount, isPrime(generate(numberCount)[0]), generate(numberCount));

                  }


                  }







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 3:02









                  cripplingdepression

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384836%2fwant-to-use-an-array-created-in-a-method-for-further-use-in-another-method%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Tonle Sap (See)

                      I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                      Guatemaltekische Davis-Cup-Mannschaft