Help on an ArrayList exercise

0

I'm trying to do an exercise where I have to use the ArrayList class and I can not find a way to do it. The statement of the exercise is as follows:

  

In a teletype terminal there is a backspace character that allows you to cancel the last character.

     

For example: If the backspace character is / , then the line    abc/d//e will be interpreted as ae .

     

There is also a null character that eliminates all the characters entered so far, suppose that character is & .

     

Perform a method that, given a strip of completed characters with * (read from the file " Source.txt "), executes the indicated operations if it encounters the character / or & . You must finally print the resulting strip.

I pass the class Teletipo.java :

package terminal_teletipo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Teletipo {
    int cont = 0;
    private String secuencia;

    File f = new File("D:\Proyectos Java\List_Interface\Fuente.txt");
    ArrayList<Character> lista = new ArrayList<Character>();

    public void File_Test() {
        System.out.println("El archivo existe? : " + f.exists() + " ");
        if (f.exists() == true) System.out.println("Direccion: " + f.getAbsolutePath());
        else System.out.println("El archivo no existe");
    }

    public void setArrayList() throws FileNotFoundException, IOException {
        FileReader r = new FileReader(f);
        BufferedReader b = new BufferedReader(r);
        secuencia = b.readLine();
        System.out.println(secuencia);
        for (int i = 0; i < secuencia.length(); i++) {
            lista.add(secuencia.charAt(i));
        }
    }

}
    
asked by Jorge DeSpringfield 11.10.2018 в 17:58
source

2 answers

0

I do not know if it is strictly necessary that you use an ArrayList but I would recommend using only a String and go through it using its methods. In any case I leave a method that has worked perfectly for me according to the description of your problem:

public void applyRules(){
    String output = ""; //esta es la salida
    int l = lista.size();
    for (int i = 0;  i < l; i++){ // recorres el arraylist
        char c = lista.get(i);    //obtienes el caracter de cada posición
        if(c >= 'a' && c <= 'z'){ //si es una letra la agregas a la salida
            output += c;                
        }else if(c == '/'){       //aqui borras el último caracter siempre que exista uno
            if(output.length() > 0)
                output = output.substring(0, output.length()-1);
        }else if(c == '&'){       //aquí borras toda la salida
            output = "";
        }else if(c == '*'){       //y si el caracter es el final detienes el ciclo
            break;
        }
    }
    System.out.println(output);
}

You only need to apply some methods of the String class and ArrayList. I recommend you read the Java API so you can learn by yourself.

    
answered by 11.10.2018 / 22:21
source
0

It's an exercise, so the idea is that the solution is created by you.
To guide you and see the problem, you should order the problem and take out the steps to follow (the 4th is just a council):

  • save all characters
  • iterate through all the characters and execute what each one indicates
  • save the result in another list
  • create a function that reads this last list and prints it to the console taking the character * as a line break
  • the first one is ready, so you need the rest
    How do I use it and execute the 'commands' according to the character?
    could be with switch
    Ex:

    List<String> clean = new ArrayList();
    int hlpr =0;
    boolean ampersand = false;
    String ayudante = "";
    for(int i=0; i<lista.size();i++){
        String caracter = lista.get(i)+"";
        switch(caracter){
        case "/":
            ayudante = this.retroceder(ayudante); /*una función para limpiar el ultimo carácter en caso de haber*/
            break;
        case "&":
            ampersand = true; /*se indica que no siga guardando hasta el salto de linea*/
            ayudante = ""; /*borra lo que se llevaba, no?*/
            break;
        case "*":
            ampersand = false;/*se indica que ahora se puede volver a guardar caracteres*/
            clean.add(ayudante); /*se guarda resultado*/
            ayudante = ""; /*se limpia para comenzar de nuevo*/
            break;
        default:
            if(!ampersand)
                ayudante+=caracter;
            break;
        }
    }
    

    could also be several if else.
    Anyway, this could also be done as the file is read and avoiding steps 1 and 2 and iterate twice for the same, but it is just a comment.

        
    answered by 11.10.2018 в 22:00