Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Searching for string values in a circular list

    Hello Everyone,

    1st and foremost I would like to greet all forum members. I am having a nightmare trying to set this program to search for string values with-in a circular list. My objective here is to input a value corresponding to a surname of class person which extends from another class called AnyClass. The result should be found or not found in list ( the surname)and a display of the record. I have already sorted out the index number search and would be ETERNALLY GRATEFULL for anybody who sorts me out in this regard.(the surname part). here under is the code. if anybody wants to paste it to an editor to test, i have grouped all classes in 1 , But the utilities file is separate.

    import java.util.*;
     
    class Utilities
    {
    	public static String randName( int n)
    		{ String name = "";   // empty string
    		  name = name + (char)randInt(65,90);
    		  for (int k = 1; k < n; k++)
    		    name = name + (char)randInt(97,122);
     		 return name;
    	 	}
    }
     
     
     import java.util.*;
     import java.io.*;
     
     
     
     
     class Node
     
     
    {  	public Node next;
    	public Person obj;
     
    	public Node( Person newObj)
    	   {   obj = newObj;
    		   next = null;
    	   }
     
    	public void show()
    		{ System.out.println(obj.getData());
    		}
     
    	public void editNode()
    		{ //obj.editData();
    		}
    }
     
     
     
     class AnyClass
     
    // Refer to T1
    {
      int seqNo;
     
    	/* ------------ Constructor (random data) ------------------*/
     
       public AnyClass( int num)
       { seqNo = num;
    	}
     
    	public AnyClass()
    	{
    	}
     
    	/* ------------ Methods ------------------*/
     
    	public String getData()
    	{ return "Sequence number: "+ seqNo +". ";
    	}
     
    	public void editData()
    	{
    	}
     
    	public boolean equals (Object keyObj)
    	{   AnyClass temp = (AnyClass) keyObj;
    	    return (temp.seqNo == this.seqNo);
    	}
     
    	public boolean equals (String key)
    		{	 return false;
    		}
    }
     
     
     class Person extends AnyClass
    {
     
       private String surname;
       int id;
     
       protected double pay;
     
     
    	public Person (String iSurn,int iIdNo, double iPay) {
    		super (iIdNo);
    		surname = iSurn;
    		id = iIdNo;
    		pay = iPay;
    	 }
    	public Person ()
    	{  }
     
     
    	double getSalary() // returns value of object's data - encapsulation (no direct access)
    	  {
    		return pay;
    	  }
     
    	void setPay (double newPay) //alterates  value of object's data - encapsulation (no direct access)
    	  {
    		 pay = newPay;
    	  }
     
     public String getData()
      {
    	  return super.getData()+"Surname: "+surname+", "+"ID No: "+id+ "Salary EUR "+getSalary();
      }
     
    /*public boolean equals(Object keyObj)
      { Person temp = (Person)keyObj;
       return (temp.seqNo==this.seqNo);//&&(temp.surname == this.surname);
       }  // maybe wrong
    */
     
    public boolean equals(String keyName)
     { return(this.surname.equals(keyName));
    }
     
     
     
     
     
    } // end of class Person
     
     
     
     class List    // Refer to notes
     
    {  	public Node head;
     
     
           	public List()
    	   	   {   head = null;
    	         }
     
     
     
     
     public String getData() // create empty CLL
            {
                if(head==null)return "Empty List";  		//if last is null, print "()"
                Node temp = head.next;				// create temporary pointer and assign to last.next to start from beginning
                String retval = "( ";				// print "(" to indicate start of CLL  print
     
                    do
                    {
                      temp.show();
                      retval= (retval + temp.obj.getData() + " ");  // print "(" + object data
                      temp = temp.next;								// incriment temp to next node
                    }
                    while(temp!=head.next);			// do this until temp has reach again the first node
                    retval+=")";						// when first node is reached, print ")"
                return "End of Process";						// return all available data
            }
     
     
     
     public boolean isEmpty()
            {
    			return (head==null);
    		}
     
     
    		public void clear()
            {
    			head=null;
    		}
     
      public Object delete()
             {
                 if(isEmpty())return ("empty list");
                    Object temp = head.next.obj;  //obj
     
                 head.next = head.next.next;
                 return temp;
             }
     
     
            public AnyClass equals(AnyClass keyObj)     // ()
             {
                 if(isEmpty())System.out.println("is Empty");
                 Node temp = head.next;
     
                       do
                       {
                         if(temp.obj.equals (keyObj)) //System.out.println("Found");
                         return temp.obj;
                         temp=temp.next;
     
                        }
                       while(temp!=head.next);
                       return null;
              }
     
              public Person equals(Person keyObj)     // ()
    		           {
    		               if(isEmpty())System.out.println("is Empty");
    		               Node temp = head.next;
     
    		                     do
    		                     {
    		                       if(temp.obj.equals (keyObj)) //System.out.println("Found");
    		                       return temp.obj;
    		                       temp=temp.next;
     
    		                      }
    		                     while(temp!=head.next);
    		                     return null;
              }
     
     
     public void reverse()
              {
                  List temp = new List();
                  Node temper = head.next;
                  if(isEmpty()){}
                  else
                     {
                       do{
                           temp.append(temper.obj);
                           temper = temper.next;
                           }
                       while(temper!=head.next);
                            head = temp.head;
                      }
              }
     
     
    	public void append(Person newObj)
           {
    		   Node temp = new Node(newObj);   //newObj
     
               if(head ==null)
                 {
                  temp.next = temp;
                  head = temp;
                 }
               else
                 {
                  temp.next = head.next;
                  head.next = temp;
                 }
             }
     
     
    }
     
     
     
    public class mainList
    {
     
     public static void main(String [] args)
     {
     
     
    List myList = new List();   // empty list
     
    		myList.getData();
     
         	for (int k = 1; k<= 10; k++)
    	    myList.append(new Person(Utilities.randName(6),k,600.00));
     
    		myList.getData();
     
     
    		System.out.println("\n");
     
     
    		System.out.println("************** Circular list Search Function*********************");
    		System.out.println("\n");
     
    		Scanner myInp = new Scanner(System.in);
    		System.out.println("Please enter value to search?");
            int i = myInp.nextInt();
     
    		AnyClass key = new AnyClass(i);
    		AnyClass temp = myList.equals(key);
     
             if( temp!= null)
                    System.out.println("Object found. Its data is: "+temp.getData());
     
               else
                     System.out.println("Object Not Found!");
     
     
        System.out.println("************** Circular list Search Function 2*********************");
    		System.out.println("\n");
     
     
    String nameInput;
     
     
    Scanner in = new Scanner(System.in);
    System.out.println("Enter name to search; ");
    nameInput = in.nextLine();
     
     
     
    	}
    }
    Last edited by KevinWorkman; April 22nd, 2011 at 12:56 PM.


Similar Threads

  1. [SOLVED] Circular Linked List---Need Help ASAP (before midnight)
    By The Dark Mathematician in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 17th, 2011, 02:24 PM
  2. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  3. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 01:31 AM
  4. 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
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM