Well I've tried for a pretty long time now to make this work. My code below is very messy and not working atm, it's mostly to show my previous attempts at this..
Soo what do I need help with? I want to create a Stack object that reads(push) character by character from a textfield and then pop and print the words in reverse. Which should be automaticly considering Stack is Last In First Out(LIFO).
charStack stack = new charStack();
String entering = textfield.getText();
for(int i = 0; i < entering.length(); i++){
//stack.push(i);
char ch = entering.charAt(i);
if(Character.isHighSurrogate(ch)){
i++;
if (i < entering.length()){
stack.push(entering.charAt(i));
}
}
stack.push(ch);
while(entering.length() != 0){
label.setText(stack.pop().toString());
}
}
The controller:
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private TextField textfield;
Stack<Character> stack = new Stack<>();
@Override
public void initialize(URL url, ResourceBundle rb) {
//label.textProperty().bind(textfield.textProperty());
}
@FXML
private void exeCute(ActionEvent event) {
// String a = textfield.getText();
//for (char c: a.toCharArray()){
// stack.push(c);
//}
//while(!stack.empty()){
// label.setText(stack.pop().toString());
//}
//char[] stack = textfield.getText().toCharArray();
//char[] reverse = new char[stack.length];
//int position = stack.length - 1;
//for(int i = 0; i<stack.length; i++){
// reverse[i] = stack[position--];
//}
//System.out.print(reverse);
//String newone = reverse.toString();
//label.setText(newone); //Works (sort of) but need to do this with stack-object
//String entering = textfield.getText();
//for (int i = 0; i < entering.length(); i++){
// char ch = entering.charAt(i);
//if(Character.isHighSurrogate(ch)){
// i++;
//if (i < entering.length()){
// stack.push(entering.charAt(i));
//}
//}
//stack.push(ch);
//System.out.print(stack.pop());
String e = textfield.getText();
for (int i = 0; i < e.length(); i++){
char ch = e.charAt(i);
if(Character.isHighSurrogate(ch)){
i++;
if (i < e.length()){
stack.push(e.charAt(i));
}
}
stack.push(ch);
//stack.pop();
//printStack(stack);
}
}
private void printStack(Stack<Character> s){
if (s.isEmpty()){
System.out.println("Stack is empty!");
}
else {
System.out.printf("%s TOP\n" + s);
}
}
}