• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on arrayList with int  RSS feed

 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me start by saying I don't know what I'm doing
I am trying to remove elements that are not prime
my logic might be broken, but I can't even make primeList.get(i) return int. I thought it would autoUnbox

This is my code Thanks for any help

 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me which line in particular you are having trouble with?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Rawley wrote:Let me start by saying I don't know what I'm doing . . .
And what makes you think we know what we are doing?

Welcome to the Ranch

I added code tags to your post, which usually make it look better, but it highlighted your inconsistent indentation and long lines. Have a look at these suggestions.
I think you need to take drastic action, by using a particular part of your computer. The off button. Get some paper a pencil and an eraser (the latter is the most important ‍) and write down how you are going to work out prime numbers. When you have done that, you can go back to the computer and rty converting your ideas into code. and not before.

A few points: I hope you are not using the Arrays class.
Lists take objects only; primitives are boxed, so you would expect the List to contain Integers. Where are you not getting an int?
Why are you starting some loops at 1 and some at 2?
Is 1 a prime number?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Rawley wrote:Let me start by saying I don't know what I'm doing ...

I don't think that's true. It's plain from your code (assuming it's yours) that you grasp the rudiments of Java but, as Campbell says, you need to StopCoding (←click). Code will never solve your problems if you don't have a clear plan of what you want to do.

I'd also say that you're trying to do too much at once. What is the actual problem here? Personally, I'd say that it's to work out if a number is prime or not, so forget all about arrays, and ArrayLists, and removing things, and deal with that:

If I'm handed a number n, how do I know if it's prime?

In Java terms, this can be re-stated as: How do I write the following method?:
public boolean isPrime(int n) { ...

If you can do that, everything else follows on from there.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, whenever you are looking for prime numbers, search for the Sieve of Eratosthenes.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the rest of the story is that I'm trying to follow Programming Methodology (Stanford) from YouTube. My current lesson is on ArrayLists. It was my understanding that you can pass an arrayList object of <type> <Integer> that you can auto-unbox to get an int. However from my testing that does not seem to work. My main purpose for my project was to try to learn ArrayLists, even if I can't get a prime number I would still like to be able to get an int, or convert Integer to int.
//Integer t = (Integer) primeList.get(i);
//println("T is "+t);
//int x = t.intValue();
I've tried many ways but am getting nowhere.

Thanks again for all the help and suggestions.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Tracy wrote:Can you tell me which line in particular you are having trouble with?


This would still be a good way to start discussing your problem. If you can't see why that is the case, you should read our FAQ entry ItDoesntWorkIsUseless. Actually it would probably be a good idea to read it anyway, just to learn good ways of asking questions on a forum.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple problems that I see here. But the main one that I would look into is the fact that you are using an ArrayList Collection object and using it like a java array, but then you are mixing in Collection methods such as the remove which then changes the size and index parameters. Which then you try to compensate by readjusting the size. This makes the problem you are trying to solve more complex than it needs to be. There is a rule that you might want to read up on called KISS.

The only way I could get this even to run was to change line 31 from 'for (int i = size; i > 0 ;i--)' to 'for (int i = size - 1; i > 0 ;i--)'. Once I did that I did not have any problems running the program. However, the results were inaccurate. The autoboxing worked just fine. I only had problems with array index out of bounds, but I fixed that by changing line 31.

Hope this helps.
Happy coding!!!
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So I kinda started over to make it easier. This will make an arrayList of the specified size and let the user pick an element to add to the special number "2" then tell you if the sum is even or odd, but it always returns odd same problem I was having before.
thanks again for all the help.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If "secretNum" has a value greater than zero, then adding something else to it which can only be zero or one can never result in zero. Did you intend for that line of code to test whether "secretNum + x" was even? If so, you'll need some parentheses to make that come out right.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right!! I did
if (((secretNum + x)%2)==0)
and it worked with even.


Thanks to everyone for all the help.

 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to keep at it, but I had to try the old code again. This time I cleaned it up and tried to better find the problem area.
program crashes on t = (primeList.get(i)); if you comment it out the loops run ok
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Rawley wrote:
program crashes on t = (primeList.get(i)); if you comment it out the loops run ok

Read the exception message to understand what went wrong and get an idea on how to fix it. You will learn a lot from reading error message.
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good call it said "ArrayList<E>.get(int) line: not available"

fixed it

Guess I need to work more on debugging

Thanks again
 
Tim Rawley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it to work

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!