Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Checking for values.  RSS feed

 
Colby Toner
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i'm currently struggling with some code and haven't seemed to find any answers. I'm trying to find a way to print to the console of an arraylist that when the user inputs a number it will tell them if that number is in the list. I think that i'm close to finding an answer but am stuck currently.
 
Liutauras Vilda
Marshal
Posts: 4647
318
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

This problem cannot be solved by coding right away. Try to solve it on a paper first. Write down the steps in english, in a form of pseudo code.

Please come up with some steps in english without using any technical terminology, what would you do, so all of us could understand?

But before that, read this tutorial (<-- link) written by Winston (moderator of CodeRanch), how to start solving such problems. Pretty much all problems should be started solving that way.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something else to consider...You do not want all your code in the main method. You should think about what needs to happen in a "big picture" kind of way, and write individual methods that each deals with one of those things. When you get into the nitty-gritty of one, you may find you need to break it down into even more methods - and that's ok.

For example, you probably need at least these methods:

1) build and populate the arraylistst
2) get user input
3) search for the value in the array
4) print out some results

The idea here is that your main method would then simply call those four methods, and nothing else (yes, I know this is not the true OO way of doing it, but we can get to that later). If someone were to come along and see that in the main method, they'd instantly know what your code is doing, and further, where they need to go to focus in on what they may need to fix/change. If the display needs to be different, they don't even have to look at the first three methods. You want to make it easy on everyone (especially yourself) who needs to ready your code in the future.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How far have you got with the exercise?
Why are you combining nextLine and parseInt? That is unnecessary; you can use Scanner#nextInt() and it will give you an int directly. If you enter wrong input, you will get an Exception.
As for how to find things in a List: your program will use a linear search because you cannot predict what order they are in. You would have to sort the List if you want to use binary search. Please go through the methods of the List interface to see what there is to use. I have given you the Java7 version of List because there were changes to interfaces in Java8.

If you are using Java8, there is a way to create a List without writing a loop. You can get a stream of ints from a Random object by calling a method. You can even instruct that method to give you results in a certain range. You cannot make a List of ints, only of Integers, and as you know converting between int and Integer is called boxing. So the IntStream has a method which does the boxing for you. You now have a Stream of Integers and that has a method to collect all its values. You need to supply a Collector for that version of that method, and the easiest way to get such a Collector is (probably) to use one of the methods of the Collectors class. I am not certain of the implementation returned by that Collector, but the last time I tried it I got a java.util.ArrayList.Please tell us how many times the value 50 will appear in your List. Note the slightly different indentation used with Streams: you make all the dots align vertically.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!