Reputation: 19
//Prime Number Calculator
import java.util.Scanner;
class PrimeNumbers {
public static void main(String[] args){
int End;
int Begin;
Scanner in = new Scanner(System.in);
//insert max value for the calculator.
System.out.println("Where should I stop?");
End = in.nextInt();
for (Begin=3; Begin<=End; Begin++){
System.out.println(Begin);
int Prime;
int PrimeList[] ;
//something is wrong around here... I don't understand what...
for (Prime:PrimeList);
PrimeList[0]=2;
if(Begin%PrimeList[Prime]!=0){
break;
}
}
}
}
I know there are many prime number calculators out there, but for school I wanted to make one but I don't know where I went wrong.
error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "Prime", Identifier expected after this token
Prime cannot be resolved to a type
Type mismatch: cannot convert from element type int to Prime
at PrimeNumbers.main(PrimeNumbers.java:25)
Upvotes: 0
Views: 281
Reputation: 213411
Ok, so your code has got a couple of problems (Ok, may be more), which I'll list here:
Problem 1:
First thing first. Always follow Java Naming Conventions. Your variable names and method names should start with lowercase alphabets. So:
int End; // Should be `int end;`
int Begin; // Should be `int begin;`
Problem 2:
You have just declared your array reference, and not initialized it. The below statement is just declaring an array reference of type int:
int PrimeList[] ;
You need to create an array object, and assign the reference to it:
int PrimeList[] = new int[size];
Problem 3:
This problem is an extension to the problem 2. Notice that, you are initializing a new array on each iteration of your loop. So, all your prime numbers would not be accumulated at the same place. Rather, your array
will clear up after each iteration. You should remove that array declaration from inside the loop.
Now, rather than adding the array declaration outside the loop, I would suggest you to use an ArrayList
instead, which is a dynamically increasing array. So, you won't have to give an initial size.
So, you can add the below declaration outside the outer for
loop:
List<Integer> primeList = new ArrayList<Integer>();
Problem 4:
Let's move ahead to your inner for
loop:
for (Prime:PrimeList);
PrimeList[0]=2;
if(Begin % PrimeList[Prime] != 0){
break;
}
}
For now, just forget what's wrong with that loop, because that loop is not needed at all. You haven't yet initialized your list
. So, there is no point in iterating over it. You rather need to initialize it with prime numbers between specified range.
So, rather than having a for loop to iterate the array / list, you should test the current number you are testing, whether it's prime or not. If it's prime, add it to the list. And to test a prime number, you would need a loop for each number. I suggest to move that logic in separate method. To check whether a number is prime or not, you keep on dividing it by numbers from 2
to num / 2
. And as soon as you see that modulus = 0
, return false
else return true
: -
public boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return false;
}
return true;
}
Now, let's get back to your original method. And take a look at your outer loop:
for (begin=3; begin <= end; begin++) {
/** For each number, check whether it's prime or not **/
if (isPrime(begin)) {
/** Is Prime, add it to list **/
primeList.add(begin);
}
}
This was it. Now, I suggest you to go through the answer step by step, and solve each problem individually.
Upvotes: 4
Reputation: 1294
You have a syntax error in your for loop. It should be
for (int prime: primeList) {
Short for loops require the type of the variable used in the iteration
Upvotes: 1
Reputation: 1263
I think You have not intialize both your variables int Prime;
and int PrimeList[];
Initialize them as
int Prime = 0;
int PrimeList[] = new PrimeList[10];
Upvotes: -1
Reputation: 13717
As stated by other you have not initialized your PrimeList
array.
Other suggestions with respect to code.
You should do validations on the input you are taking. Check Exception handling for the same. For example, if the user didn't enter a number where you had expected one then your code will break.
The variable names are not in a java-bean specification format i.e. the first letter should be in small caps and then should follow the camelCase notation which is to capitaize first letter of each next word in the variable name.
The algo seems to be completely wrong. PrimeList is not being populated anywhere, unless you have generated them in a separate code and didn't integrate that in the above given code.
Upvotes: 1
Reputation: 37576
You have to initate your Array, for example:
int PrimeList[] = new int[3]
Upvotes: -1