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];
}
}
java methods
|
show 1 more comment
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];
}
}
java methods
Probably because you return after the first iteration of the outer for loop. Also if this is anisPrime()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 returnq[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 haveisPrimeaccept a single int, and not an entireArray. Also you will find that you will need to utilize abreakorreturnstatement ifitIsever becomes false so the result will not get overridden
– GBlodgett
Nov 20 at 1:32
|
show 1 more comment
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];
}
}
java methods
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
java methods
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 anisPrime()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 returnq[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 haveisPrimeaccept a single int, and not an entireArray. Also you will find that you will need to utilize abreakorreturnstatement ifitIsever becomes false so the result will not get overridden
– GBlodgett
Nov 20 at 1:32
|
show 1 more comment
Probably because you return after the first iteration of the outer for loop. Also if this is anisPrime()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 returnq[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 haveisPrimeaccept a single int, and not an entireArray. Also you will find that you will need to utilize abreakorreturnstatement ifitIsever 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
|
show 1 more comment
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.
add a comment |
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));
}
}
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 at 2:40
Bejond
872515
872515
add a comment |
add a comment |
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));
}
}
add a comment |
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));
}
}
add a comment |
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));
}
}
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));
}
}
answered Nov 20 at 3:02
cripplingdepression
1
1
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
isPrimeaccept a single int, and not an entireArray. Also you will find that you will need to utilize abreakorreturnstatement ifitIsever becomes false so the result will not get overridden– GBlodgett
Nov 20 at 1:32