File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Linked List and recursion help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Linked List and recursion help" Watch "Linked List and recursion help" New topic

Linked List and recursion help

Matt Ryann

Joined: Dec 05, 2001
Posts: 1
Hi everyone,
Need some help with a couple of problems.
1. I have to incorporate a linked list and its methods I have made into another class for an assignment. Keep getting non-static method .... cannot be referenced from a static context.
2. Am trying to write a recursive method that will return a) the total track time for a CD & b) the total track time for the entire catalogue. Both codes for question are below,
thanks in advance!!
1. Put this method
class CDList
private CDNode head;
private TrackNode trackHead;

public CDList()
head = null;
trackHead = null;

//addCD() method: Will add CD to list
public void addCD(String artist, String title)
CDNode newNode = new CDNode(artist, title);
CDNode current;

//if no other CD's exist add to head
if (head == null)
{ = head;
head = newNode;

//when other nodes already present
current = head;
while ( != null)
current =; = newNode;
into this other class & method
//first part of class
// 'a' pressed -- add a cd function
case 'a':
//the first part of the method
public static void addCD()
String artistName;
String cdTitle;

// prompt for the name of the artist and the cd's title
System.out.print("\nEnter the artist's name: ");
artistName = safeReadLine();

System.out.print("\nEnter the CD title: ");
cdTitle = safeReadLine();

// ***********************************************
// here you know both the title and artist name of
// the CD you have to create
// artistName -- String holding the artist name
// cdTitle -- String holding the cd's title
// ***********************************************
etc etc.
2. This is my attempt at the recursive method to calc the track time, isn't working (have included nodes for clarity)
class CDNode
protected String cdTitle;
protected String cdArtist;
Track trackHead;
CDNode next;

public CDNode (String title, String artist)
cdArtist = artist;
cdTitle = title;
trackHead = null;
next = null;
class TrackNode
protected String trackTitle;
protected int trackLength;
TrackNode next;

public TrackNode (String title, int length)
trackTitle = title;
trackLength = length;
next = null;
public int CDTime(TrackNode current)
current = trackHead;
int result = 0;

if (current == null)
return result;

return result + current.trackLength + CDTime(;

Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
Your error message should be giving you the clue, somewhere in a static method you are calling an instance method without an object reference.
i.e. you are doing 'method()' and compiler expects 'obj.method()'.
It may be that you should change 'method()' to be static OR change the static method to be an instance method.
In class TrackNode what is the purpose of 'int result' if it's value never changes ? Where is 'trackHead' defined ?
[This message has been edited by Colin Kenworthy (edited December 06, 2001).]
I agree. Here's the link:
subject: Linked List and recursion help
jQuery in Action, 3rd edition