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