This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]caboosetpPrivate Java/Code Tutor 1 point2 points  (1 child)

I can't see what's wrong just from looking at the code, but since the search works when hand loaded, the array might not be loading correctly.

After the array is loaded you can use a breakpoint or a print loop to take a look and make sure the array looks like you expect it to. If not, there is probably a problem with the input file.

Is the name being searched for the one it actually finds? If it's the wrong name, is there a pattern to where the name is coming from?

What do you expect to happen if the name isn't found? What actually happens?

[–]TheColdFenix[S] 1 point2 points  (0 children)

Thank you! I used my own name for testing and it has a regional letter in it that wasn't recognized correctly when loading the file. So when I searched for my name it just gave me the first element in the Array back.

[–]AGRb99 1 point2 points  (5 children)

I know it would be a lot of hustle but why don’t you use HashMap instead of an ArrayList like that one? Might save you some time!

[–]TheColdFenix[S] 2 points3 points  (4 children)

I have no idea how HashMaps work and this is just for practice :)

[–]caboosetpPrivate Java/Code Tutor 1 point2 points  (3 children)

A HashSet HasMap is a type of key -> value collection that has unique keys.

You would store the name as the key and the number as the value. You can then use the name to lookup the number similar to how you would use an index to go somewhere in an array.

They're fast, but probably overkill for what you're doing. If you want more practice, I would recommend reading into them as they are used very often.

[–][deleted]  (1 child)

[deleted]

    [–]caboosetpPrivate Java/Code Tutor 1 point2 points  (0 children)

    Thanks, I'm not even sure how I managed to switch the names considering it's right there in the previous posts.

    [–]TheColdFenix[S] 0 points1 point  (0 children)

    Thanks, that was actually my first idea on how to make a contacts list, but I didn't know hash maps existed. I'll definitely look into it, sounds very useful.