0

The FOR condition generates an endless printout. I really don't know how that can happen. There is only one entry as an instance in Books. What did I do wrongly?

package LOIoefeningen;

import java.util.ArrayList;


public class Handin 
{


        public static void main(String[] args) 
        {


            String title = null;
            String author = null;




            Possessor Bookworm   = new Possessor("Bookworm", new Books(title, author));

            ArrayList<Books> book = new ArrayList<>(); 


          for (int i = 0; i < book.size(); i++)
          {

          book.add(new Books("Pietje Puk", "Henri Arnoldus"));
          System.out.println(book.get(i));

          }      

        }
    }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • You are adding book every time you are in loop. Thus book.size() keeps on increasing making it a moving target and resulting in infinite loop – deadzg_devil Oct 29 '19 at 23:46
  • @deadzg_devil see my comment below!! but thanks!! in the first place!! –  Oct 29 '19 at 23:50
  • Do you maybe also know how to get ride of the hexadecimal output? –  Oct 29 '19 at 23:53
  • Also, when fixing you will want the loop to be `i < book.size();` – Scary Wombat Oct 29 '19 at 23:53
  • Override `toString` in book to get rid of the hex output. – SephB Oct 29 '19 at 23:56
  • @SephB sorry I don't get that, can you explain closer? –  Oct 30 '19 at 00:01
  • @ScaryWombat got that already. But thanks!! –  Oct 30 '19 at 00:01
  • @twannemann I was not 100% sure what you where meaning, but assumed when you print `book.get(i)` you get an ugly hex string of it's class and address. If you override toString in the Books class you will get what ever you return. ` @Override public String toString() { return ; }` – SephB Oct 30 '19 at 00:06
  • @SephB well still don't get it. Do you want me to add somewhere in the class Book' 'toString'? or do you want me to remove 'toString'?? anyway the code 'toString' as a plain text does not occur in the class Books. The term override is not clear to me. –  Oct 30 '19 at 00:13
  • @SephB is meaning https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Scary Wombat Oct 30 '19 at 00:20
  • @SephB and Scary Wombat... it doesn't seem to work. It's pretty unclear to me how to resolve the problem. –  Oct 30 '19 at 00:31
  • @twannemann Dont radically change your question. Now all comments and answers do not make sense for people looking at this in the future. – Scary Wombat Oct 30 '19 at 00:32
  • BTW `public Books(String t, String s) toString()` is totally meaningless. As per the link I gave you the `toString` method should be something like `public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }` – Scary Wombat Oct 30 '19 at 00:33
  • @ScaryWombat, guess you are right. But I can post only one question in 90min. But I didn't overlook the consequences indeed. –  Oct 30 '19 at 00:34
  • You could have edited tour question. which I suggest you do now before this question is downvoted more and deleted – Scary Wombat Oct 30 '19 at 00:37
  • @SephB and Scary Wombat... now I got it. It works!!! thank. And I will change my question as it was originally. –  Oct 30 '19 at 00:40
  • so maybe upvote and/or accept @SephB answer – Scary Wombat Oct 30 '19 at 00:51

1 Answers1

0

You are adding a new book each time through the loop. Move book.add(new Books("Pietje Puk", "Henri Arnoldus")); before the loop.

SephB
  • 617
  • 3
  • 6