|
||
|
|||
|
I'm getting the following error. The idea is to have the user input a number, and then print out all the primes less than the number.
Java Code
Enter a value: 9 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Primes.main(Primes.java:18) Here is my code Java Code
import java.util.*;
public class Primes {
public static void main(String[] args){
Scanner stdin = new Scanner(System.in);
int x = 0;
boolean isPrime = true;
int ARRAY = 0;
int primes[] = new int[ARRAY];
System.out.println("Welcome to Prime Calculator!");
System.out.println("Enter a value: ");
x = stdin.nextInt();
for(int i = 2; i < (x/2) && isPrime == true; i++){
if (x % i == 0){
isPrime = false;
} else {
primes[ARRAY] = i;
ARRAY++;
}
}
for(int i = 0; i < ARRAY; i++){
System.out.println(primes[0]);
}
}
}
|
|
|||
|
I read a lot about that, and I feel like since we haven't learned about it, that's not what is expected in the program. My algorithm may not be the most efficient, but it seems like it should make sense? If the remainder of something divided by 2 = 0 then its not prime, if it is, try 3, if that is 0, then it's not prime, if it is, try 4, and so on..
Substituting x into the array got rid of the error message, but now onlys prints the number 2--sometimes once, sometimes twice, sometimes three times |
|
||||
|
That's part of the fun of computer science: learning about new (well, in this case really old) algorithms that perform a given task.
The reason why you're algorithm isn't working is because you're checking the primality of x. If you want to go about it that way, I'd recommend writing a helper function that will check to see if a number is prime, then everytime you find a prime number, print it out. Java Code
for (int i = 2; i < x; i++)
{
if(isPrime(i))
{
System.out.println(i + " is prime");
}
}
Java Code
for every integer i from 2 to the number: if the number is divisible by i, return not prime end return true
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple java program to calculate wall covering of a room | parvez07 | What's Wrong With My Code? | 3 | 22-08-2009 08:31 PM |
|
100 most searched terms
Search Cloud
|
| 2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps |