File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "" Watch "" New topic

Alec Balles

Joined: Jun 06, 2012
Posts: 2
I was looking for some help on one of my projects. I believe the first half is correct, but there are some complications in my second one. My professor is not giving me the help I need, any and all help will be much appreciated. It must be something small i'm missing.

1. Write a static method sortAndRemoveDuplicates that accepts a list of integers as its parameter and
rearranges the list's elements into sorted ascending order, as well as removing all duplicate values from
the list. For example, the list {7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9} would become {-9, -5, 4, 7, 8,
11, 15, 27, 32} after a call to your method. Use a Set as part of your solution.
2. Write a static method is1to1 that accepts a Map<String, String> as its parameter and returns true if the
two keys map to the same value. For example, {Darwin=206-9024, Hawking=123-4567, Newton=123-
4567, Smith=949-0504} should return false, but {Darwin=206-9024, Hawking=555-1234, Newton=123-
4567, Smith=949-0504} should return true. The empty map is considered 1-to-1 and returns true.

My code:

Greg Charles

Joined: Oct 01, 2001
Posts: 2969

Hi Alec, welcome to JavaRanch!

First some boring admin type stuff. Please use code tags when posting code, and also use proper indentation. That makes your code much easier to read. (I've added the code tags for you here.) Second, choose the right forum. You posted this in our Applets forum, but really it belongs in one of the regular Java forums. I'll move it to Beginning Java for you. Finally, you need to explain your problem. All you say is you think it's not correct, but why do you think that? Does it not compile? Is the output wrong? Be as specific as possible.

All these things will make it much more likely that the other ranchers here will read and respond to your question. Thanks!
Alec Balles

Joined: Jun 06, 2012
Posts: 2
You mean the tags?

and the output is
[7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9]
[-9, -5, 4, 7, 8, 11, 15, 27, 32]
it's returning false, when it should be returning true. Maybe i'm using == when I should be using ()? because I am comparing two things.
I'll repost this in the right section.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Alec, please BeForthrightWhenCrossPostingToOtherSites

luck, db
There are no new questions, but there may be new answers.
Greg Charles

Joined: Oct 01, 2001
Posts: 2969

The first output shows your unsorted and unfiltered ArrayList, because your sortAndRemove... method creates a new object and returns that, but you don't do anything with the return value. The second time you call it, you do save the return value, a TreeSet, and print that out, so you get sorted and filtered results.

For your is1to1() method, you should be using equals(), not ==, because you're comparing objects. Also, you need to compare one value with all subsequent values, not just one. Don't use two while loops with the same iterator. Also, reverse your logic; you should return false if you find a match.
I agree. Here's the link:
jQuery in Action, 3rd edition