0

The while loop should compare the ibsn's of these two objects. Objects being compared:

  list[0] = new ReadingMatter ("Words and Stuff", "9-082-1090-1");
  list[1] = new Magazine ("Fashion", "318921019", "Mike Dales");
  list[2] = new Book ("Rocks and Minerals", "3-323-0691-2", "Jamie Dawson");
  String[] mainCharacters = {"Lennie","George","Candy"};
  list[3] = new Novel ( "Of Mice and Men", "4-569-2190-1", "John Steinbeck", mainCharacters);
  list[4] = new TextBook ("Java, Java, Java", "3-131-9871-0", "John Smith", true);

the changed compareTo:

public int compareTo(Object other)
{
    ReadingMatter temp = (ReadingMatter)other;
    int result = 0;

    if (this.isbn.compareTo(temp.getIsbn()) > 0)
        result = -1;
    else if (this.isbn.compareTo(temp.getIsbn()) < 0)
        result = 1;

    return result;
}

the while loop

while(testing)
  {
     testing = false;

     for(int j = 0; j < list.length - 1; j++)
     {
        if (list[j].compareTo(list[j+1]) > 0)
          {
           temp = list[0];
           list[0] = list[1];
           list[1] = temp;

           testing = true;   
          }
     }
  }

is it something to do with the hyphenated numbers? how would i get around the hyphens?

EDIT: The problem is that the loop is infinite, the if statement is always being used

5
  • 3
    what's the question ? Commented Mar 1, 2014 at 21:32
  • You haven't included enough detail for a satisfactory answer. What's the problem you're having? Where is the code for the isbn.compareTo method? Do Magazine and Book extend ReadingMatter? Commented Mar 1, 2014 at 21:33
  • 1
    You should parameterize your Comparable; what is more it can be rewritten as return other.isbn.compareTo(isbn); Commented Mar 1, 2014 at 21:33
  • I'm not sure what your problem/question is but your if/for is wrong: when j is the last valid index you compare the last item with the next one which doesn't exist. Commented Mar 1, 2014 at 21:33
  • yeah they extend it. oh you basically answered it. I forgot to build the isbn.compare thank you! Commented Mar 1, 2014 at 21:36

2 Answers 2

1

The problem is that you always switching index 0 and 1 but checking index j and j+1:

if (list[j].compareTo(list[j+1]) > 0)
{
    temp = list[0];
    list[0] = list[1];
    list[1] = temp;

    testing = true;   
}

Therefore if you have one indexpair j, j+1 with j>2 to which compareTo delivers >0 you get your infinite loop.

Sign up to request clarification or add additional context in comments.

3 Comments

i have 5 in the array, i just edited them out from simplicity
and you're still always swapping list[0] with list[1]. All the other elements in the list never even get sorted, so testing is always true at the end of the loop. That's your answer.
Sorry. I am still not allowed to edit this answer. I tried to print beneath a corrected version. The upper version is to point out his problems.
0

Since your conditional only orders elements 0 and 1, you will end up with list[0] < list[1] < list[2] which sets testing=true and loops, again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.