I'm trying to make a simple program that uses a scanner for input and has a while loop that continues taking input until an end character is entered. I want the scanner to take input and add a string to the stack list. I'm trying to figure out why this code doesn't terminate the while loop when a space is typed.
import java.util.Scanner;
public class ReverseString<E> {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Stack<String> stack = new Stack();
//while(scan.hasNext() && !stack.equals(" ")){
// while(!scan.nextLine().equals("")){
while (scan.hasNext()) {
stack.push(scan.next());
if (scan.equals(" ")) {
break;
}
}
System.out.print(stack.pop() + " ");
}
}