Results 1 to 3 of 3
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Method not working as expected?

    Hello, I am implementig a simple Linked list data. However, my remove method is not working... kind of strange. It works in the LinkedList.java`s main but not in Main.java, which constructs private statics. Here is the code: (I`ll list the key problem features only)

    package bg.test.ilian.List;

    public class LinkedList<E> implements ListManipulations<E> {

    //inner class Node
    //List has node
    public class Node<T> {
    T data;
    Node next = null;
    Node(T t) {
    data = t;
    next = null;
    }
    public T getData() { return (T)data; }
    public boolean hasNext() { return this != null; }
    T getT() { return data; }
    public String toString() { return getT().toString(); }
    Node() {
    data = null;
    next = null;
    }
    } //END INNER


    Node<E> reference = null;
    Node<E> head = null;
    Node<E> current = null;
    E[] elements ;
    public LinkedList() {
    reference = new Node<E>();
    head = new Node<E>();
    current = new Node<E>();
    }

    public void delete() {
    if ( isEmpty() ) {
    System.out.println("List is empty");
    } else {
    while ( ! isEmpty() ) {
    try { this.removeFront(); } catch (Exception e) {}
    }

    }
    }

    public Node<E> find(E s) {
    reference = head.next;
    Node<E> t = null;
    while ( reference != null ) {
    if ( reference.data == s ) t = reference;
    reference = reference.next;
    }
    return t;
    }

    private Node remove(Node n, E d) {
    if ( n.data == d ) {
    n = n.next;
    } else {
    n.next = remove(n.next, d);
    }
    return n;
    }
    public void removeFront() {

    head.next = head.next.next == null ? null : head.next.next;
    }


    @Override
    public void add(E t) {
    // TODO Auto-generated method stub
    Node<E> n = new Node<E>(t);
    n.next = head.next;
    head.next = n;

    }
    public int size() {
    Node<E> tmp = head.next;
    int i=0;
    while ( tmp != null ) {
    i++; tmp = tmp.next;
    }
    return i;
    }
    @Override
    public E get(E next) {
    // TODO Auto-generated method stub
    return null;
    }
    @Override
    public E get(int ind) throws java.lang.ArrayIndexOutOfBoundsException {

    if ( this.toArray()[ind] != null ) return (E)this.toArray()[ind].toString();

    return null;
    }


    public void print() {
    Node<E> tmp = head.next;
    while ( tmp != null ) {
    System.out.println(tmp.toString());
    tmp = tmp.next;
    }
    }
    @Override
    public boolean hasNext() {
    // TODO Auto-generated method stub
    return head.next != null;
    }

    @Override
    public Object[] toArray() {
    // TODO Auto-generated method stub
    if ( this.isEmpty() ) return null;
    /*else*/
    Object[] a = (E[]) new Object[this.size()];
    Node<E> t = head.next;
    int i =0;
    while ( t != null ) {
    a[i] = t;
    t = t.next; i++;
    }
    return (E[]) a;
    }

    public boolean isEmpty() {
    return head.next == null;
    }

    public static void main(String... args) {

    LinkedList<String> ll = new LinkedList<String>();
    ll.add("HUI1");
    ll.add("HUI2");
    ll.add("HUI3");
    ll.add("HUI4");
    ll.add("HUI5");
    ll.add("HUI6");
    ll.add("HUI7");


    // ll.removeFront(); ll.removeFront();
    System.out.println("FInd...:"+ll.find("HUI3").toSt ring());
    ll.remove("HUI3");
    ll.print();
    ll.delete();

    } //END MAIN

    @Override
    public void remove(int i) {
    // TODO Auto-generated method stub

    }

    @Override
    public E remove(E a, E b) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void remove(E e) {
    // TODO Auto-generated method stub
    try {
    head.next = remove(head.next, e);
    } catch (Exception ex) { }
    }

    @Override
    public void remove() {
    // TODO Auto-generated method stub

    }
    }


    And the Main.java which just creates some user interaction

    package bg.test.ilian.List;
    import java.util.Scanner;

    public class Main {
    static boolean isRunning = true;
    static LinkedList<String> names = new LinkedList<String>();
    public static void add(String n) {
    names.add(n);
    }
    public static String get(int i) {
    return names.get(i);
    }
    public static void remove(String which) {
    try { names.remove((String)which); }
    catch (Exception e) { System.out.println("ERROR:"+e.toString()); }
    }
    public static void debugAdd(int N) {
    for (int i=0; i < N; i++ ) {
    names.add(i+"d");
    }
    }


    public static void printMenu() {
    Scanner input = new Scanner(System.in);
    String choice ="";
    while ( isRunning ) {
    System.out.println("Enter (A) to add (P) to print (R) to delete or (G) to get by index");
    choice = input.nextLine();
    switch ( choice ) {
    case "d": case "D": System.out.print("Enter N elements:");
    int N = Integer.parseInt(input.nextLine()); debugAdd(N); break;
    case "r": case "R": System.out.print("Enter stirng to delete:");
    String c = input.nextLine(); remove(c); break;
    case "A":
    case "a": System.out.print("Enter element:"); String element = input.nextLine(); names.add(element); break;
    case "P":
    case "p": System.out.println("Printinig..."); names.print(); break;
    case "G":
    case "g": try {

    System.out.print("Enter number:");
    String elementbyid = input.nextLine();
    System.out.println(names.get(Integer.parseInt(elem entbyid)));
    }catch (Exception e) { System.out.print("ERROR - no index: "+e.toString()); }
    break;
    case "q":
    case "Q": System.out.println("Exiting..."); isRunning = false; names.delete(); break;
    default: System.out.println("UNRECOGNIZED COMMAND"); break;

    }
    }
    }
    public static void main(String[] args) {
    Main.printMenu();
    }
    }


    The main problem is that actually the remove method called by Main.java is not wrokig, while LinkedList`s remove method is working fine, by finding a specific element and removing it. Any clues, or what I might got wrong?
    P.S. I completely forgot,.... how was the code hilightnig tag?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default Re: Method not working as expected?

    [code][/code]

    And where is your interface?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Method not working as expected?

    The interface is not quite useful to post, since the implementation works but here you are:
    Java Code:
    package bg.test.ilian.List;
    
    public interface ListManipulations<T> {
    
    	public void add(T t);
    	public T get(T next);
    	public T get(int index) throws Exception;
    	public boolean hasNext(); 
    	public Object[] toArray(); 
    	public void print(); 
    	public void remove();
    	public void remove(int i);
    	T remove(T a, T b); 
    	public void remove(T e);
    	
    }

Similar Threads

  1. Parse int not working as expected
    By codyjava in forum New To Java
    Replies: 7
    Last Post: 11-03-2013, 08:16 PM
  2. replaceAll(string a, string b) not working as expected
    By Bronsonboy in forum New To Java
    Replies: 3
    Last Post: 07-16-2012, 01:18 AM
  3. ugh another method that isn't working
    By jjth39347 in forum New To Java
    Replies: 14
    Last Post: 04-17-2011, 10:57 PM
  4. Gridbaglayout not working as expected
    By berkeleybross in forum AWT / Swing
    Replies: 7
    Last Post: 07-06-2010, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •