Out of Bound Error - Stumped


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Out of Bound Error - Stumped

  1. #1
    Join Date
    Jul 2008
    Posts
    3

    Question Out of Bound Error - Stumped

    I'm new to java and I have a program to write a function that returns an array of int; the only parameter to that function should be an int and the array returned should be of size this parameter, and should contain the int first prime numbers.

    It compliled fine, but I get an out of bound error when trying to run the program:

    public class PrimeNumbers {

    public static void main(String [] args){

    int primeCount = Integer.parseInt(args[0]);

    ArrayList<Integer> primes = new ArrayList<Integer>();

    for(int i=0; primes.size() < primeCount; i++)
    if(isPrime(i))
    primes.add(i);

    for(int i : primes)
    System.out.print(i+ ", ");
    System.out.println();

    int numberOfPrimes = 4;
    numberofPrimes-=1;
    System.out.println(numberOfPrimes.get(prime));

    }

    public static boolean isPrime(int n) {

    if(n <= 2) return n == 2;

    if(n % 2 == 0) return false;

    for(int i = 3; i<=(int)Math.sqrt(n); i+=2)
    if (n % i == 0) // if no remainder, is not prime.
    return false; // return false

    return true;
    }
    }

    Am I even on the right track???

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Can you tell where you are getting the out of bounds error (what statement is being executed)?

  3. #3
    Join Date
    Jul 2008
    Posts
    3
    Not to sure. Here's the error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at primenumbers.PrimeNumbers.main(PrimeNumbers.java:20)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Is the code you have posted here an exact replica of the code in your java program? If so, you have forgotten to set aside a scope for your loading the arrayList with prime numbers loop, before you go to print out the contents of primes. You were getting the ArrayOutOfBounds exception in your print loop since, by your test for "primeness", n = 0 returns FALSE, so you have nothing in primes when you go to print. If you establish the scope of your loop as follows, that error should go away:

    Code:
    for(int i=0; primes.size() < primeCount; i++)
    {
         if( isPrime(i) )
         {
              primes.add(i);
         }
    }
    
    for(int j : primes)
    {
         System.out.print(j + ", ");
    }
    
    System.out.println();
    You have now told the compiler/interpreter that you want to fill primes with as many prime numbers as the user has instructed before you print.

    I suggest (to avoid confusion) that you use j (or another name other than "i") for your temporary int being extracted from primes for printing in your extended for loop, and yet another name for the loop variable in your isPrime method's for loop.

    You need to work on your isPrime method, too - the for loop needs to be given defined structure.
    Last edited by nspils; 07-19-2008 at 03:40 PM.

  5. #5
    Join Date
    Jul 2008
    Posts
    3
    I added you suggestion and I still get the error:

    import java.util.ArrayList;

    public class PrimeNumbers {

    public static void main(String[] args) {

    int primeCount = Integer.parseInt(args[0]);

    // Array of primes
    ArrayList<Integer> primes = new ArrayList<Integer>();

    // Check and add if prime
    for (int i = 0; primes.size() < primeCount; i++) {
    if (isPrime(i)) {
    primes.add(i);
    }
    }

    for (int j : primes) {
    System.out.print(j + ", ");
    }

    System.out.println();


    // Display nth prime
    int numberOfPrimes = 4;
    numberOfPrimes -= 1;
    System.out.println(primes.get(numberOfPrimes));

    }

    public static boolean isPrime(int n) {
    // Two is the lowest and only even prime
    if (n <= 2) {
    return n == 2;// Two is only even prime number
    }
    if (n % 2 == 0) {
    return false;// initialization : starting at 3
    // test condtion: while less than or equal to number's square root
    // incrementor: Add 2 to check odd numbers : i+=2 : 3,5,7,9...
    }
    for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
    if (n % i == 0) // if no remainder, is not prime.
    {
    return false; // return false
    }
    }
    return true;
    }
    }

    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at primenumbers.PrimeNumbers.main(PrimeNumbers.java:20)
    Java Result: 1

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Have you tried printing to the screen the following information as debugging statements, to test whether the system is seeing any arguments, is assigning the value of primeCount, and populating the primes ArrayList?

    number of arguments ( System.out.println(args.size(); )
    content of args[0] ( System.out.println(primeCount); )
    number of prime numbers in prime ( System.out.println( primes.size(); )

    In your java file, what is the statement on line 20?

Similar Threads

  1. pleeese help me i am sooo stuck
    By christopher in forum Java
    Replies: 4
    Last Post: 12-19-2002, 12:31 PM
  2. Replies: 6
    Last Post: 04-15-2002, 02:05 PM
  3. Bound Date Field and blank values
    By Chris Brown in forum VB Classic
    Replies: 2
    Last Post: 09-14-2001, 04:40 AM
  4. Check box isssue when bound to a database column
    By Narayan in forum VB Classic
    Replies: 0
    Last Post: 04-10-2001, 10:30 AM
  5. Clearing out a bound datagrid
    By Joe in forum VB Classic
    Replies: 2
    Last Post: 06-02-2000, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center