Hey , for my class I have to create an Infinite Integer class which holds can hold a really, really large integer using a doubly linked list. It holds 3 integers per node, so 5,697,343 would be held like
[5] [697] [343]. I implemented my constructor and toString method fine, but I'm having trouble with my compareTo method which compares one Infinte Int to another. My logic and code seems right but the method always returns 0, heres the code-

Edit: It also seems that the size of the DLL is always 0, I dont know whats causing it as the .size() method works fine in the other methods.

public int compareTo(Object o)
	{


		System.out.println();

		InfiniteInt passedInInt;
		passedInInt = (InfiniteInt) o;

		DLLNode ptr1 = this.head;
		DLLNode ptr2 = passedInInt.head;
		int compareInt = 0;

		int breakInt = this.size();

		if(this.size() > passedInInt.size())
			compareInt = 1;

		else if(this.size() < passedInInt.size())
			compareInt = -1;

		else
		{

			System.out.println("--------------");
			System.out.println(this + " this is whats inside the first DLLNode" );
			System.out.println(passedInInt + " this is whats inside the other DLLNode");
			System.out.println(this.size() +" this is the size of the first DLLNode");
			System.out.println(passedInInt.size() + " this is the size of the other DLLNode");

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

				System.out.println(ptr1);

				System.out.println(ptr2);

				if((Integer)ptr1.data > (Integer)ptr2.data)
				{
					compareInt = 1;
					i = breakInt;
				}

				else if((Integer)ptr2.data > (Integer)ptr1.data)
				{
					compareInt = -1;
					i = breakInt;
				}

				ptr1 = ptr1.next;
				ptr2 = ptr2.next;
			}
		}
		return compareInt;
	}

}

It seems like you would have to do a few things to compare two Infinite Integers.

1. Does one have more nodes than the other? If so, the one with more nodes is greater, since each node holds 3 numbers.
2. Do they have the same number of nodes? If so, start at the top node for each, and compare the numbers. If one node's integers are greater than the other's, the greater one is your answer. Otherwise, move on to the second node (for each Infinite Integer)

It doesn't seem to me that your code is doing these things.

1. Here was my code for the larger DLL scenario-

if(data.size() > passedInInt.size())
			compareInt = 1;

		else if(data.size() < passedInInt.size())
			compareInt = -1;

2. And here was my for-loop

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

				if((Integer)ptr1.data > (Integer)ptr2.data)
				{
					compareInt = 1;
					i = breakInt;
				}

				else if((Integer)ptr2.data > (Integer)ptr1.data)
				{
					compareInt = -1;
					i = breakInt;
				}

				ptr1 = ptr1.next;
				ptr2 = ptr2.next;
			}

Anyways, I did some more debugging and the problem is that the passed in InfiniteInt is always 0, I think it stems from my toString() method, because I use removeFirst() instead of getFirst(), heres my toString() code-

public String toString()
		{
			String commaString= "";
			Object node;
			Object node2;
			int num;
			int realSize = data.size();
			System.out.println("This is the size of the DLL before toString() " + realSize);

				for( int i = 0; i < realSize; i++)
				{
					if( i == 0)
					{
						node = data.removeFirst();
						commaString = commaString + node.toString();

						System.out.println(commaString);
					}

					else
					{
						num = Integer.parseInt((String)data.getFirst());
						System.out.println(num);

						if(num < 10)
						{
							commaString = (commaString + ",00" + num);

							System.out.println(commaString);
						}

						else
						{
							node2 = data.removeFirst();
							commaString = (commaString + "," + node2.toString());

							System.out.println(commaString);
						}
					}
				}
			System.out.println("This is the size of the DLL after toString() " + data.size());
			return commaString;
		}

So then I tried to create a toString that didn't involve removeFirst() but ran into problems, heres that code-

public String toString()
		{
			String commaString= "";
			DLLNode node;
			int num;
			int realSize = data.size();


			node = data.head;
				for( int i = 0; i < realSize; i++)
				{
					if( i == 0)
					{

						commaString = commaString + node.toString();

						System.out.println(commaString);
					}

					else
					{

						if((Integer)node.data < 10)
						{
							commaString = (commaString + ",00" + node.toString());

							System.out.println(commaString);
						}

						else
						{

							commaString = (commaString + "," + node.toString());

							System.out.println(commaString);
						}
					node = node.next;
					}
				}

			return commaString;
	}

The problem occurs at this part

if((Integer)node.data < 10)

its not a compile time error but I get a ClassCastException

So that should tell you something. A node and an Integer are not the same data type so you shouldn't be trying to force them to be.

Well the data inside the node is a string, I thought I was able to change a string to an Integer, but if not, how do I check to see whats inside the node is less than 10? In a desperate attempt I tried to make a node with string "10" inside and I cant use the < sign for those types.

You can use Integer.parseInt(yourString) to convert a String to an Integer. But I'd recommend you think about my advice earlier. . I don't think there is an easier way to accomplish your goal.

Ok I tried a couple of stuff but I'm at a loss here, I'm not sure how to check if the node data is less than 10 so I can add "00" to the string without forcing it, any tips would be appreciated

It's hard to say because I don't know what your class looks like for Infinite Int

I asked my professor the same question and this is his response-

"Right now, the DLList node's data will hold an Object. That is because you are USING a new DLList in your InfiniteInt class, but are not using Generics, so it just figures it will hold an Object.

But it logically SHOULD hold an int, and that is accomplished by having it hold things of type Integer. So when you use a DLList, be sure it is defined to only contain <Integer> and ints should all work automatically as data.

Also, check the requirements. Your InfiniteInt class is both a subclass of DLList and has data that is a DLList. You should do one or the other. Usually (in my opinion), having it as the data is preferable (it limits the use of the class to what you specifically define as its methods), but I believe that the requirements said to make it a subclass this time.
So using Generics should help out with this problem..."

Heres part of my InfinteInt class-

import java.util.*;


public class InfiniteInt <E extends Integer> extends DLList  //implements Comparable
{

	//DLList  data = new DLList();

	public InfiniteInt(String str)
	{
		String mod1String;
		String mod2String;
		int size;
		size = str.length();

		if(size % 3 == 0)
		{
			for(int i = 0; i < size; i = i + 3)
			{

				int place = i + 3;
				this.addLast(str.substring(i, place));
			}

		}

		else if ( size % 3 == 2)
		{
			this.addLast(str.substring(0, 2));
			mod2String = str.substring(2);

			for(int i = 0; i< mod2String.length(); i = i + 3)
			{

				int place = i + 3;
				this.addLast(mod2String.substring(i, place));
			}

		}

		else if( size % 3 == 1)
		{
			this.addLast(str.substring(0,1));
			mod1String = str.substring(1);

			for(int i = 0; i < mod1String.length(); i = i + 3)
			{

				int place = i + 3;
				this.addLast(mod1String.substring(i, place));
			}


		}
	}

	public InfiniteInt()
	{
		this.addFirst(0);
	}

	public String toString()
	{

		String commaString= "";
		DLLNode ptr;
		int num;
		int realSize = this.size();


		ptr = this.head;
			for( int i = 0; i < realSize; i++)
			{
				if( i == 0)
				{

					commaString = commaString + ptr.toString();

					System.out.println(commaString);
				}

				else
				{

					if(ptr.data < 10)
					{
						commaString = (commaString + ",00" + ptr.toString());

						System.out.println(commaString);
					}

					else
					{

						commaString = (commaString + "," + ptr.toString());

						System.out.println(commaString);
					}
				ptr = ptr.next;
				}
			}

		return commaString;
	}

I removed

DLList  data = new DLList();

and replaced "data" with "this" through out the code. But I still run into the same problem

If you're still asking for help with your question, "Ok I tried a couple of stuff but I'm at a loss here, I'm not sure how to check if the node data is less than 10 so I can add "00" to the string without forcing it, any tips would be appreciated", you're going to need to post the DLList class. Your InfiniteInt class extends DLList, but how am I going to tell you how to check the data without knowing what type of Object the data is, which requires seeing the DLList class? If the "data" Object that you keep referring to is simply a String, then use what I told you above.

Don't use generics for your InfiniteInt class; it will *always* be a DLL of type Integer [or in general Number]. Instead create your InifiniteInt class by extending the DLL class and specifying Integer as the type parameter.

class DLL<T> {
  // A DLL of objects of type T
}
class Infinite extends DLL<Integer> {
  // integers expressed as DLL of Integer objects
}

Also, how do you propose to handle negative integers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.