skoon
skoon

Reputation: 23

searching a stack for the name that comes first alphabetically

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;
}

the program is getting in an infinite loop

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

thank you in advance

Upvotes: 1

Views: 152

Answers (4)

Mark Yao
Mark Yao

Reputation: 408

May be you misunderstand your teacher mean.

Reference code:

import java.util.Collections;
import java.util.Comparator;
import java.util.Stack;

public class StackTest {
public static void main(String[] args) {
    Stack<String> stack = new Stack<String>();
    stack.push("Tiger Woods");
    stack.push("Jack Nicholas");
    stack.push("Arnold Palmer");
    stack.push("Jack Nicholas");
    stack.push("Jimmy Demerrit");
    stack.push("Jack Nicholas");
    stack.push("Sam Sneed");
    stack.push("Jimmy Demerrit");
    stack.push("Ben Hogan");
    stack.push("Jimmy Demerrit");
    stack.push("Walter Hagen");
    stack.push("Tommy Armor");
    stack.push("Bobby Jones");
    Collections.sort(stack, new Comparator<String>() {
        public int compare(String o1, String o2) {
            // the method compareTo in String class
            return o2.compareTo(o1);
        }
    });
    //the sorted stack
    System.out.println(stack);
    System.out.println(stack.pop());
   }
}

Output

// Last In First Out or First In Last Out
[Walter Hagen, Tommy Armor, Tiger Woods, Sam Sneed, Jimmy Demerrit, Jimmy Demerrit,        Jimmy Demerrit, Jack Nicholas, Jack Nicholas, Jack Nicholas, Bobby Jones, Ben Hogan, Arnold Palmer] 

Arnold Palmer

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

Iterate the collection and return search element.

 for(String s:names)
 {
  if(s.startsWith("c")) // or if(s.charAt(0)==letter)
     {
      return s;
      }
 }
 return null;

Upvotes: 0

thagorn
thagorn

Reputation: 747

If you need to retain the stack you should copy it first but this will find the alphabetically smallest name.

public String findFirst() {

        String name = null;
        while(!names.isEmpty()) {
            if(names.peek().compareTo(name) < 0) {
                name = names.pop();
            } else {
                names.pop();
            }
        }
        return name;
}

Upvotes: 0

Philippe
Philippe

Reputation: 9762

For the infinite loop: when you test for name.charAt(0) == letter, if that is true, you don't pop from the names stack, so you'll keep running into that instance (and pushing and pushing into temp).

Note that fixing this alone won't make it work like you'd want. For instance, you should find it suspicious that you never update the value of letter.

Also, you shouldn't count on the fact that looking only at the first letter is enough. If you want to compare two strings with respect to alphabetical ordering, you can use the .compareTo method on Strings.

Upvotes: 1

Related Questions