This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arraylist contains this number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist contains this number?" Watch "Arraylist contains this number?" New topic

Arraylist contains this number?

jonas sorensen

Joined: Jan 23, 2013
Posts: 1
Hello people.
Srry for asking, but i've been trying for the last four hours and i'm not moving forward.

I'm programming this bingo thing. It has to check if a number exists in the arraylist. If it does, it should get a new number NytTal();

I've also tried with equals, but dosent seem to work either. I've made an system outprint;

[80, 11, 14, 14]
Equals: false
Contains: false

I've even tried with bought trukkettal and trukkettal2

what seems to be wrong here? i just have a simple arraylist and then some adds from actionperformed...

Tony Docherty

Joined: Aug 07, 2007
Posts: 2173
Your ArrayList is declared as containing String values but your if statement is checking if the ArrayList contains an Integer (the int value will be AutoBoxed to an Integer).
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
Welcome to the Ranch

I am afraid your five lines of code fill me with dread. And not about finding things in the List.
Why are you using a method which returns a String and parsing it to a number? Why did you not return a number?
Why are you trying to put things into a List, and then check whether they have already been added?
Why are you calling that code from a Listener? Why are you not developing it as a standalone application, which you will later wrap in a GUI? That is a far better way to develop applications. Your application should have an interface so you can do something like this:-I think there is a piece of hardware getting between you and the program. Unfortunately it is the computer, and I think you will have to put it aside. Instead, use a two‑dimensional carbohydrate‑based flexible writing surface¹.
Write down how you would do it is real life, with a bag of balls. See whether you can change the bag of balls to a Java collections implementation. I presume you are familiar with the random number methods in Java? (They are not really random, but you won’t notice the difference.)

¹ = sheet of paper
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7552

jonas sorensen wrote:It has to check if a number exists in the arraylist.

OK, but, as Tony said, that's NOT what you're doing.

Strings are NOT numbers. In fact, in your case (and many others) StringsAreBad (←click).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
I agree. Here's the link:
subject: Arraylist contains this number?
Similar Threads
enhanced for loop hangup?
Head First Java - pg 135 - ArrayList and Compile Errors
Unexpected Type error
override the equals and hashcode
lost n confussed