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 4 of 4

Thread: Can someone please tell me why my code doesn't print out anything?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone please tell me why my code doesn't print out anything?

    Ok here's my main and printlinked method:
    import java.util.Scanner;
     
    public class ListTest
    {
    	public static void main(String []args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		ListNode head = new ListNode();
    		ListNode tail = head;
    		head.link = null;
    		System.out.print("Enter integers, seperated with blanks, to put into a linked list (type zero (0) to end): ");
    		int in;
    		in = keyboard.nextInt();
    		while (in != 0)
    		{
    			ListNode x = new ListNode();
    			x.link = null;
    			x.data = in;
    			tail = x;
    		}
    		printLinked(head.link);		
    	}
     
    	public static void printLinked(ListNode head)
    	{
    		System.out.println("The numbers in your linked list are:");
    		while (head != null)
    		{
    			System.out.print(head.data + " ");
    			head = head.link;
    		}
    	}
    }
    And here's the ListNode.java:
    public class ListNode
    {
    	int data;
    	ListNode link;
    }
     
    It builds fine but when I run it this part won't work:
     
    	public static void printLinked(ListNode head)
    	{
    		System.out.println("The numbers in your linked list are:");
    		while (head != null)
    		{
    			System.out.print(head.data + " ");
    			head = head.link;
    		}
    	}
    PLEASE help me
    Last edited by helloworld922; February 4th, 2010 at 04:12 PM. Reason: please use [code] tags!


  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: Can someone please tell me why my code doesn't print out anything?

    You forgot to link up every newly created node to the head.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my code doesn't print out anything?

    I thaught it was something like that but I can't figure out how to do it?? Brain freeeeezeee!

  4. #4
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Can someone please tell me why my code doesn't print out anything?

    Hi I am looking into your code..

    will debug and let you know

Similar Threads

  1. [SOLVED] why does this code print 0.0 for the value and "F" as the letter grade?
    By etidd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2010, 10:38 PM
  2. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM
  3. Is it possible to print a spade, club, heart, or diamond in java?
    By BC2210 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2009, 08:35 PM

Tags for this Thread