I am trying to make a program that through conditions entered by keyboard, I generate a series of numbers, the program would be more or less like this:
If the user enters a number between 0 and 9 it adds it to the list, if the user enters "D" it deletes the last digit of the list, if the user enters "C" and a number, it deletes the last elements of the list according to the number you entered, if you enter "B" and a number, delete the digit that is in the position that indicates the number entered from right to left, if you enter "M" it shows the series of numbers so far, the program ends if the user enters "F".
The problem is that I have the code but it does not print what it should and I can not find the error. As an example income:
9 8 7 6 5 D B 2 M 4 3 two one C 3 B 4 M F and I would have to return two lines, one with 986 and another with 864.
Do not be so hard on me, I'm new to programming!
import java.util.*;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> lista = new LinkedList<>();
Iterator<Integer> iterador;
Scanner entrada = new Scanner(System.in);
int x, y, i, m;
String n;
while (true) {
n = entrada.next();
if (n.equals("D")) {
if (lista.size() != 0) {
lista.removeLast();
}
} else if (n.equals("C")) {
y = entrada.nextInt();
if (y <= lista.size()) {
for (i = lista.size() - 1; i == y; i--) {
lista.removeLast();
}
}
} else if (n.equals("B")) {
x = entrada.nextInt();
if (x <= lista.size()) {
lista.remove(lista.size() - x );
}
} else if (n.equals("M")) {
for (i = 0; i < lista.size(); i++) {
System.out.print(lista.get(i));
}
} else if (n.equals("F")) {
break;
} else {
m = Integer.parseInt(n);
lista.add(m);
}
}
}
}