Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Having trouble redirecting nodes

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Having trouble redirecting nodes

    Hey everyone,

    I am working on an assignment that concerns itself with linked lists. Below is the code for two user-defined classes. The class Node contains the instance variables, constructors, accessors, mutators and print method. The class LinkedList is where I need to write methods for starting the list, inserting a new node, deleting a node, and searching the nodes. Each node contains professor information(which is why I have left the arguments as empty strings when creating them (prof1 and prof2))).

    So far I have created the three empty nodes head, last, and temp,which serve only as pointers. I am trying to create the list and have created the node prof1. My question is how do I redirect head to point to prof1 instead of null?. If I write head = prof1 I get the error "Syntax error on token";", , expected" which is indicated on the semicolon of Node prof1 = new Node("", "", "", null);



    public class Node {
     
    	//icams
     
    	//instance variables
    	private String name;
    	private String researchArea;
    	private String email;
    	private Node link;
     
    	//constructors
    	public Node() {//default constructor
     
    		name = "";
    		researchArea = "";
    		email = "";
    		link = null;
    	}
     
    	public Node(String nameIn, String researchIn, String emailIn, Node newLink) {//constructor with parameters.
     
    		name = nameIn;
    		researchArea = researchIn;
    		email = emailIn;
    		link = newLink;
    	}
     
     
     
     
     
    	//accessors
    	public String getName() {
     
    		return name;
    	}
     
    	public String getResearchArea() {
     
    		return researchArea;
    	}
     
    	public String getEmail() {
     
    		return email;
    	}
     
    	public Node getNode() {
     
    		return link;
    	}
     
     
     
     
     
    	//mutators
    	public void setName(String nameIn) {
     
    		name = nameIn;
    	}
     
    	public void setResearchArea(String researchIn) {
     
    		researchArea = researchIn;
    	}
     
    	public void setEmail(String emailIn) {
     
    		email = emailIn;
    	}
     
    	public void setNode(Node linkIn) {
     
    		link = linkIn;
    	}

    public class LinkedList extends Node {
     
    	Node head = new Node();
    	Node last = new Node();
    	Node temp = new Node();
     
    	Node prof1 = new Node("", "", "", null);
    	head = prof1;
    	//Node prof2 = new Node("", "", "", null);
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble redirecting nodes

    You're trying to code outside of any method, which isn't possible. Java technically lets you initialize in the class itself, but that's BAD practice (except for constants).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    KingLane (October 18th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble redirecting nodes

    Thank you, helloworld922. That is a pretty obvious mistake that I should have caught.

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Having trouble redirecting nodes

    I might sound like a bit of a noob now but how is it bad practise initialising fields in the class rather than the constructor?

    Sorry for going off topic.

    // Json

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble redirecting nodes

    Organization. That's the point of a constructor, to properly set up the object so it can follow it's "contract" (what it's suppose to do). Also, you can do much more inside a constructor than a one-line initialization. Also, chances are some fields are going to inherently require inputs, which that type of initialization is impossible.

    The practice is not so bad on static/final fields, though. I still prefer to initialize these type of fields outside of any method

  7. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Having trouble redirecting nodes

    I've always thought that as long as you're consistent either way is fine

    // Json

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble redirecting nodes

    I'm talking about multi-person projects. You'll almost always have a constructor in a class (even if it takes no arguments), and the first (and in many cases the last) place I look is there. Granted, it's not the worst thing you can do (probably why they still allow this syntax)

Similar Threads

  1. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM
  2. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM
  3. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  4. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM