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

Thread: Searching for string values in a circular list

  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 01:56 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Searching for string values in a circular list

    When posting code, please use highlight tags to preserve formatting. I added them for you this time.

    Also, that's way too much code for us to go through. We just don't have time. For better help sooner, create an SSCCE that demonstrates what you've tried and where you're stuck.

    So, where are you stuck exactly?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Searching for string values in a circular list

    Thanks for the reply KevinWorkman. My apologies for the length of code. Here under is a print screen of what i have done till this point. Basically this is the breakdown in words.

    class AnyClass just contains an integer sequence number and has methods to put data into a string.It also has 2 methods called equal to be used to search value of integer number in main program.

    class Person extends AnyClass and its methods( SUPPOSEDLY !) it also adds a further Id, a surname and a pay amount.

    class list creates the circular list with its methods

    main program aim is to populate circular list with objects of class person(working). Then a sequence number search of the list ( WHICH IS WORKING) and finally a surname search of class person ( equals method) which is driving me bananas. My shot at these methods can be seen in the code, but i dont think i am on the write track. Please help
    [QUOTE]Thanks for the reply KevinWorkman. My apologies for the length of code. Here under is a print screen of what i have done till this point. Basically this is the breakdown in words.

    class AnyClass just contains an integer sequence number and has methods to put data into a string.It also has 2 methods called equal to be used to search value of integer number in main program.

    class Person extends AnyClass and its methods( SUPPOSEDLY !) it also adds a further Id, a surname and a pay amount.

    class list creates the circular list with its methods

    main program aim is to populate circular list with objects of class person. Then a sequence number search of the list ( WHICH IS WORKING) and finally a surname search of class person ( equals method) which is driving me bananas. My shot at these methods can be seen in the code, but i dont think i am on the write track. Please help
    [IMG]C:\Documents and Settings\User\My Documents\ps.bmp[/IMG]

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Searching for string values in a circular list

    Still, I'm not sure where the problem is. You say you're having trouble with the equals() method? What exactly is giving you trouble?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Ankit.Java.King (July 11th, 2011)

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

    Default Re: Searching for string values in a circular list

    Thanks Kevin workman. Problem solved. here under is what solved it.took me some time but did the trick .Will post just to share with those that might have followed the problem.


    public String equals(String keyObj) // ()
    {
    if(isEmpty())System.out.println("is Empty");
    Node temp = head.next;

    do
    {
    if(temp.obj.equals (keyObj))
    return keyObj;

    temp=temp.next;

    }
    while(temp!=head.next);
    return null;
    }

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Searching for string values in a circular list

    Interesting. Usually equals() methods return a boolean. Your method looks more like a get() or a find() function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Searching for string values in a circular list

    @KEVIN WORKMAN. This is the corresponding code in the main program.


    System.out.println("************** Circular list Surname Search*********************");



    String nameInput;


    Scanner in = new Scanner(System.in);
    System.out.println("Enter surname to search; ");
    nameInput = in.next();



    String key1 = nameInput;
    String temp1 = myList.equals(key1);

    if( temp1!= null)
    System.out.println("Surname found");


    else
    System.out.println(" No such Surname!");

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Searching for string values in a circular list

    Yeah I understand how you're using the method, it's just a little misleading because equals() is a very specific method in the Object class, and it is overridden for a different purpose. It's fine as long as it works, but I would have called the method something else. To each their own.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Searching for string values in a circular list

    Agreed on the name. BUT that equals is building from a class then onto another and then onto the list. This is an assignment and I had to play according to the rules.

    Have another headache as we speak, editing the contents in the list with an editData() method. I can edit no problem when i assign the person class with declared Persons such as Person newperson = (new Person("Workman",1,600.00)) but NOT when the circular list is populated with a loop such as

    for(int k=1;k<=5;k++)
    myList.append(new Person(Utilities.randName(6),k,600.00));

    any Hints??

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Searching for string values in a circular list

    Hmm, I'm not sure without an SSCCE. What happens instead of what you'd expect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Searching for string values in a circular list

    Quote Originally Posted by hippo1974 View Post
    Agreed on the name. BUT that equals is building from a class then onto another and then onto the list. This is an assignment and I had to play according to the rules.
    I would put it far more strongly than KevinWorkman did - the equals(..) method is one of Java's fundamentals. To reinterpret it, particularly in such a counter-intuitive way, is a semantic error. The meaning of 'equals' is universally an assertion of equality, not an arbitrary search method. If you feel the assignment expects this misnomer, I suggest you have misinterpreted the requirements; I find it hard to believe anyone could set an assignment that expects such a semantic abuse. YMMV.

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, 03: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, 03:38 AM
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 11:40 AM