• Post Reply Bookmark Topic Watch Topic
  • New Topic

Copying One Array To Another  RSS feed

 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, what I'm trying to do is to accept elements in one wrapper array and check if the no is prime then copy that array element from wrapper array to the prime array. and display prime array. Below is the code which I have typed but its throwing an exception saying
java.lang.ArrayIndexOutOfBoundsException
. What can I do for this? Please help me to figure out where I am going wrong. Here is the code:



Thanks all!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out the value of i and j whenever you read from or write to an array element.
If you ever get a value of 5 or more that's where your problem is.
The stack trace of the exception should tell you which line is causing it as well.
 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the while loop to this



Am I correct or any other changes??
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pranit Sonawane wrote:Am I correct or any other changes??

Does it work properly now ?
 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No am afraid. There is no change. Am new to this actually so.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked out which line is throwing the exception yet ? It tells you in the stack trace.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pranit Sonawane wrote:I have changed the while loop . . .
That sounds as if you are simply guessing. You might guess the right code, but that is barely more likely than winning £5000000 on the lottery.
You need to write down very simply what you intend that loop to do.
Also write down the values of i, j, (which you ought to declare as local to the loops like this: for (int i = 1;…) as you go through the loops.

Also, are you using % 2 == 0 to test for prime numbers? That is incorrect; what happens when you pass 9 or 15 or 21?
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Also, are you using % 2 == 0 to test for prime numbers? That is incorrect; what happens when you pass 9 or 15 or 21?


Actually it's worse than that. The posted code identifies 2, 4, 6, 8, 10, 12, and so on as prime numbers.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least 2 is a prime number!
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's true, 1 out of infinity is a pretty good average.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!