Link List - can't pass an object through

[b][red]This message was edited by epimetheus at 2006-9-30 7:12:0[/red][/b][hr]
So in class we have to write a silly program utilizing a linked list of general objects containing data. In this program, we have to emulate a donut shop and boxes of doughnuts. The user enters several types of doughnuts (no limit other than at least 1) in the command line. Each string is sent to the Doughnut constructor which sets the string as type and initializes the doughnuts as boxes with 12 doughnuts in each box.

When I send a string, such as l.addFront(args[i]); it works just fine, but when I do the same command with l.addFront(new Doughnut(args[i])); it says it puts something into the list, but when I print it, all I get is null. (l.size()) returns the number of items I entered, say 4 for my test runs, but l.toString() returns null.

I put my code up on my webpage so not to take up lots of space here: It is three different files, one in each table. http://adhochaven.net/files/lab3.html (edited- forgot to include link)

I have other things I have to write in, my driver program is just a test program, the other one involves decrementing the doughnuts in each box, and removing the box from the list when it is zero. But for now, I can't even get it to load up and print my lists, can anybody help me?

-epimetheus (who is apparently too ignorant to get a simple thing like this to work)


Comments

  • : [b][red]This message was edited by epimetheus at 2006-9-30 7:12:0[/red][/b][hr]
    : So in class we have to write a silly program utilizing a linked list of general objects containing data. In this program, we have to emulate a donut shop and boxes of doughnuts. The user enters several types of doughnuts (no limit other than at least 1) in the command line. Each string is sent to the Doughnut constructor which sets the string as type and initializes the doughnuts as boxes with 12 doughnuts in each box.
    :
    : When I send a string, such as l.addFront(args[i]); it works just fine, but when I do the same command with l.addFront(new Doughnut(args[i])); it says it puts something into the list, but when I print it, all I get is null. (l.size()) returns the number of items I entered, say 4 for my test runs, but l.toString() returns null.
    :
    : I put my code up on my webpage so not to take up lots of space here: It is three different files, one in each table. http://adhochaven.net/files/lab3.html (edited- forgot to include link)
    :
    : I have other things I have to write in, my driver program is just a test program, the other one involves decrementing the doughnuts in each box, and removing the box from the list when it is zero. But for now, I can't even get it to load up and print my lists, can anybody help me?
    :
    : -epimetheus (who is apparently too ignorant to get a simple thing like this to work)
    :
    :
    :
    Did you define the toString() or took the default function? The default function for an Object returns nothing if memory serves me well.

  • : Did you define the toString() or took the default function? The default function for an Object returns nothing if memory serves me well.
    :

    The toString() I am using at this point is inside my List class:

    public String toString(){
    String s = "";
    Node p = head;

    while(p != null){
    s += p.data + " ";
    p = p.next;
    }
    return s;
    }
  • :
    : : Did you define the toString() or took the default function? The default function for an Object returns nothing if memory serves me well.
    : :
    :
    : The toString() I am using at this point is inside my List class:
    :
    : public String toString(){
    : String s = "";
    : Node p = head;
    :
    : while(p != null){
    : s += p.data + " ";
    : p = p.next;
    : }
    : return s;
    : }
    :
    Did you check if the various references are filled using the debugger? It looks like either the head is not filled or the data objects aren't.

  • : Did you check if the various references are filled using the debugger? It looks like either the head is not filled or the data objects aren't.
    :

    Both my Doughnut class and List class had toString methods, and for some reason l.toString() was printing from my Doughnut class rather from my List class. I hadn't had anything written for my toString method in my doughnut class so I just had it return null; I changed it an problem solved, l.toString() prints all the objects data.
  • : : [b][red]This message was edited by epimetheus at 2006-9-30 7:12:0[/red][/b][hr]
    : : So in class we have to write a silly program utilizing a linked list of general objects containing data. In this program, we have to emulate a donut shop and boxes of doughnuts. The user enters several types of doughnuts (no limit other than at least 1) in the command line. Each string is sent to the Doughnut constructor which sets the string as type and initializes the doughnuts as boxes with 12 doughnuts in each box.
    : :
    : : When I send a string, such as l.addFront(args[i]); it works just fine, but when I do the same command with l.addFront(new Doughnut(args[i])); it says it puts something into the list, but when I print it, all I get is null. (l.size()) returns the number of items I entered, say 4 for my test runs, but l.toString() returns null.
    : :
    : : I put my code up on my webpage so not to take up lots of space here: It is three different files, one in each table. http://adhochaven.net/files/lab3.html (edited- forgot to include link)
    : :
    : : I have other things I have to write in, my driver program is just a test program, the other one involves decrementing the doughnuts in each box, and removing the box from the list when it is zero. But for now, I can't even get it to load up and print my lists, can anybody help me?
    : :
    : : -epimetheus (who is apparently too ignorant to get a simple thing like this to work)
    : :
    : :
    : :
    : Did you define the toString() or took the default function? The default function for an Object returns nothing if memory serves me well.
    :


    The toString() method in Object returns wht is more or less the memory location(at least in how the JVM sees it) of the object.
    [italic][blue]Just my 2 bits[/blue][/italic]

  • :
    : : Did you check if the various references are filled using the debugger? It looks like either the head is not filled or the data objects aren't.
    : :
    :
    : Both my Doughnut class and List class had toString methods, and for some reason l.toString() was printing from my Doughnut class rather from my List class. I hadn't had anything written for my toString method in my doughnut class so I just had it return null; I changed it an problem solved, l.toString() prints all the objects data.
    :

    Posting code would have been more helpful in trying to help fix your problem
    [italic][blue]Just my 2 bits[/blue][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories