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

Thread: help, help

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help, help

    I am new to programming, so please accept my apologies if this question is simple.

    My teacher wants us to create a method that searches a stack for the name that comes first alphabetically and returns it

    for example : If I have these names in my stack:

    Tiger Woods, Jack Nicholas, Arnold Palmer, Jack Nicholas, Jimmy Demerrit, Jack Nicholas, Sam Sneed, Jimmy Demerrit, Ben Hogan, Walter Hagen, Tommy Armor, Bobby Jones

    the method should return Arnold Palmer

    here is my code :

    public String findFirst()
    {
        	System.out.println("findFirst" );
     
        	Stack<String>temp = new Stack<String>();
     
        	char letter = 'A';
        	String name = null;
     
    		while(!names.isEmpty())
    		{
    			name = names.peek();
     
    			if(name.charAt(0) == letter)
    			{
    				temp.push(name);
    				name = names.peek();
    			}
    			else
    			{
    				temp.push(name);
    				names.pop();
    			}
     
    		}
     
    		while(!temp.isEmpty())
    		{
    			names.push(temp.pop());
    		}
     
        	return name;
    }

    Please help me for I need to know how to fix it for my program

    thank you in advance

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: help, help

    What is the problem? Does the code compile? Are there exceptions? Does it not work correctly and if not, what do you expect and what do you get? I recommend reading the link in my signature entitled 'getting help'

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,378
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help, help

    What are the steps the program should take to solve your problem? Can you make a list of the steps in English?

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help, help

    the program should find the firs name in the stack that starts with the letter 'A'

    the problem is that the program is getting in an infinite loop

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,378
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help, help

    Do you know which loop is the problem?
    What is the condition that keeps the loop going? How can you change that condition to let the loop exit?
    the program should find the firs name in the stack that starts with the letter 'A'
    That is an objective of the program. What are the steps needed to do that?