6 Replies - 233 Views - Last Post: 10 April 2012 - 06:59 PM Rate Topic: -----

#1 kevorski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 29-January 12

Problem with linked list (just need to get started)

Posted 10 April 2012 - 04:19 PM

Hey guys, I've been working on this homework assignment and I'm just stuck really bad.

I'm having a hard time trying to get this working

So here are the files that I have. What I'm trying to do is take a MutableString object that is from a string. It will then take the object and create a LinkedList based off the characters.


This is my Tester
import java.util.*;

public class MutableStringTester
{
	public static void main(String[] args)
	{
		MutableString string = new MutableString();
		Scanner kb = new Scanner(System.in);
		
		int choice = 0;
		
		while(choice != 9)
		{
			choice = displayMenu(kb);
			
			if(choice == 1)
			{
 				String newString = "Hellow World";
 				MutableString string = new MutableString(newString);
				System.out.println(newString);
			}
			if(choice == 2)
			{
				System.out.println();
				
				System.out.println();				
			}
			if(choice == 3)
			{
				System.out.println();			
				
				System.out.println();				
			}
			if(choice == 4)
			{
				System.out.println();
				
				System.out.println();
			}			
			if(choice == 5)
			{
				System.out.println();				
				
				System.out.println();
			}			
			if(choice == 6)
			{
				System.out.println();				
				
				System.out.println();
			}
			if(choice == 7)
			{
				System.out.println();				
				
				System.out.println();
			}
			if(choice == 8)
			{
				System.out.println();
				
				System.out.println();
			}
		}
		
		System.out.println();		
		System.out.println("Goodbye!");
	}
	
	private static int displayMenu(Scanner kb)
	{
		int choice = 0;
		while (choice < 1 || choice > 9)
		{
			System.out.println("Please make a valid selection");
			System.out.print("1) Create a new string\n" +
								  "2) Sort the string\n" +
								  "3) Print the string\n" +
								  "4) Print in reverse order\n" +
								  "5) Create a substring\n" +
								  "6) Delete the string\n" +
								  "7) Delete all instances of character\n" +
								  "8) Compare with another string\n" +
								  "9) Quit\n");
		
			choice = kb.nextInt();
			kb.nextLine();
		}
		
		return choice;
	}
	
	private static String readString(Scanner kb)
	{
		System.out.print("Please enter a string: ");
		String s = kb.nextLine();
		
		return s;
	}
}



This is my MutableString class file
public class MutableString//  implements Comparable
{
	private LinkedList theString;
	
	public MutableString()
	{
		this.theString = new LinkedList();
	}
	
	public MutableString(String theString)
	{
		this.theString = new LinkedList();
	}
}




This is my LinkedList class file
import java.util.*;

public class LinkedList
{
   public class LinkList
   {
      private class Node
      {
         private char data;
         private Node next;
      
         public Node(char data)
         {
            this.data = data;
         }//end node
			
      }//end Node
   
      int size;
      Node head;
	}
	public void addAll(char string)
	{
		add(string);
	}//end addAll

	//adds data to the end of the link list
	public void add(char data)
	{
		Node curr = head;
		Node newNode = new Node(data);
		if(head == null)
		{
			head = newNode;
		}//end if
		else
		{
			while(curr.next != null)
			{
				curr = curr.next;
			}//end while
			
			curr.next = newNode;
		}//end else
		
		size++;
	}//end add
		
	public String toString()
	{
		String s = "";
		if(head == null)
		{
			s = "The list is empty";
		}//end if
		for(Node curr = head; curr != null; curr = curr.next)
		{
			s = s + curr.data + "\n";
		}//end for
		return s;
	}//end toString
}




The issue is that the LinkedList file isn't recognizing anything. Any help would be much appreciated. I'm having a really difficult time on this. It will print out the memory location of the linked list, but I can't get the characters into the Linked List.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with linked list (just need to get started)

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 04:45 PM

I don't think you could have come up with a more oxymoronic class name. I couldn't resist. :stuart:

This post has been edited by blackcompe: 10 April 2012 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 05:32 PM

public class MutableString//  implements Comparable
{
	private LinkedList theString;
	
	public MutableString()
	{
		this.theString = new LinkedList();
	}
	
	public MutableString(String theString)
	{
		this.theString = new LinkedList();
	}
}



What is the difference between the 2 constructors ?
And naming a LinkedList "theString" may not be a good idea, quite confusing
Was This Post Helpful? 0
  • +
  • -

#4 kevorski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 29-January 12

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 06:00 PM

View Postpbl, on 10 April 2012 - 05:32 PM, said:

public class MutableString//  implements Comparable
{
	private LinkedList theString;
	
	public MutableString()
	{
		this.theString = new LinkedList();
	}
	
	public MutableString(String theString)
	{
		this.theString = new LinkedList();
	}
}



What is the difference between the 2 constructors ?
And naming a LinkedList "theString" may not be a good idea, quite confusing



one sends in a parameter of a string
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 06:09 PM

View Postkevorski, on 10 April 2012 - 09:00 PM, said:

one sends in a parameter of a string

Yes, but you don't use it so what the use of it ?
Was This Post Helpful? 0
  • +
  • -

#6 kevorski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 29-January 12

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 06:14 PM

View Postpbl, on 10 April 2012 - 06:09 PM, said:

View Postkevorski, on 10 April 2012 - 09:00 PM, said:

one sends in a parameter of a string

Yes, but you don't use it so what the use of it ?


i was trying to. to send it the string to split it up
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Problem with linked list (just need to get started)

Posted 10 April 2012 - 06:59 PM

How do you expect the program to work, when you really haven't done anything to make it work? You've got what looks to be a fully working linked list, yet you never add anything to it...

public MutableString(String s) {
     list = new LinkedList();
     for(char c: s.toCharArray())
          list.add(c);
     System.out.println(list);
}



You should have MutableString.toString return list.toString.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1