Advertisement
coelhojs

EditorTexto (exemplo de vetor de string)

Sep 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4.  
  5. public class EditorTexto {
  6.     String[] array;
  7.     int n;
  8.  
  9.     EditorTexto() {
  10.         this(50);
  11.     }
  12.  
  13.     EditorTexto(int tamanho) {
  14.         array = new String[tamanho];
  15.         n = 0;
  16.     }
  17.  
  18.     void inserirFim(String x) throws Exception {
  19.         if (n >= array.length) {
  20.             throw new Exception("Erro!");
  21.         } else if (x == "#") {
  22.             removerFim();
  23.         } else if (x == "@") {
  24.             array = null;
  25.         } else {
  26.             array[n] = x;
  27.             n++;
  28.         }
  29.     }
  30.  
  31.     String removerFim() throws Exception {
  32.         if (n == 0) {
  33.             throw new Exception("Erro!");
  34.         } else {
  35.             return array[--n];
  36.         }
  37.     }
  38.  
  39.     void limpar() {
  40.         for (int i = 0; i < n; i++) {
  41.             array[i] = "";
  42.         }
  43.         n = 0;
  44.     }
  45.  
  46.     void mostrar() {
  47.         System.out.print("[ ");
  48.         for (int i = 0; i < n; i++) {
  49.             System.out.print(array[i] + " ");
  50.         }
  51.         System.out.println("]");
  52.     }
  53.  
  54.     public static void main(String[] args) throws Exception {
  55.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  56.         EditorTexto editor = new EditorTexto(50);
  57.  
  58.         while (editor.n <= editor.array.length) {
  59.             System.out.println("Digite o caractere e pressione ENTER: ");
  60.             String caractere = br.readLine();
  61.             switch (caractere) {
  62.             case "#":
  63.                 editor.removerFim();
  64.                 editor.mostrar();
  65.                 System.out.println();
  66.                 break;
  67.             case "@":
  68.                 editor.limpar();
  69.                 editor.mostrar();
  70.                 break;
  71.             default:
  72.                 editor.inserirFim(caractere);
  73.                 editor.mostrar();
  74.             }
  75.  
  76.         }
  77.         System.out.println("Editor cheio!");
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement