in Education by
I have a class Item, which implements Comparable, and has a compareTo method. I want to compare the object o to other Items. I casted o to Item. In a separate class, Inventory, I have a method for inserting items into the inventory. But I only want to insert if their product numbers are different. So I try to call the compareTo() method to compare item numbers but get a stackoverflow error. I've tried p.compareTo(iter.next), because I want it to cycle through all of the items in the list. Sorry the formatting isn't perfect. first post here. public class item{ public int compareTo(Object o){ result = compareTo((Product)o); if (result < 0){ return -1; } else if (result == 0){ return 0; } else{ return 1; } } } public class ProductInventory extends Product { private void insert(Product p){ Iterator iter = list.iterator(); if (list.size() == 0) { list.addFirst(p); } while(iter.hasNext()) { p.compareTo(iter.next()); //if (p.getNumber() != iter.next().getNumber()) { System.out.print("RESULT:" + result); if (result != 0) { list.addFirst(p); } else { System.out.print("DUPLICATE"); } iter.next(); } } I want it to print duplicate if result = 0 (the numbers are the same), otherwise add it to list. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The compareTo method in your item class is infinitely recursing on this line: result = compareTo((Product)o); //this is a self-call! You want to replace that line with the implementation for your compareTo, for example: result = this.value - that.value; //insert actual logic here

Related questions

0 votes
    The problems relating to the environment are inherently of a global nature. How? Please answer the above question....
asked Aug 4, 2022 in Education by JackTerrance
0 votes
    What is the value returned by function compareTo() if the invoking string is less than the string compared? ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is the value returned by function compareTo() if the invoking string is greater than the string compared ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which one of the following is used to define the structure of the relation, deleting relations and ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Let's say, I have a folder called maps and inside maps I have map1.txt, map2.txt, and map3.txt. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I have to Update the Xml document object generated using Apache XMLbeans.There are two ways I am trying to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I want to get java.lang.Class for Scala package object: app/package.scala: package object app { } ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Can anybody tell me how to send a mail to group of person by using JavaMail? I have tried but I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Basically i have a java String which i want to restrict all the characters other than this regex code ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    Hei guys, I'm having some troubles with % ... So here's the code: String random = "%RANDOM%"; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I'm trying to create a local temp bigquery table in memory for testing purposes. I'm doing this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have two modules. App and module Test. Module app contains MainActivity with button. Module Test contains ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    In my application when the current logged in user's usernam is changed i want to update the UserDetails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
...