segunda-feira, 10 de abril de 2017

Pesquisando - marcando e substituindo caracteres em array

Se facilmente conseguimos pesquisar,
marcar e remover ocorrências
de caracteres num array multidimensional
de char, também podemos substituí-los certo?
não duvide, veja meu exemplo agora.

Abaixo imagens do programa em execução:



Veja abaixo o código do programa:



import java.util.Scanner;
public class PROJETO {
     private static int Pesq ( ) {
         J jht = new J ( );
        jht.textbackground ( jht.WHITE );
      jht.Moldura ( ( short ) 1, ( short ) 24, ( short ) 2, ( short ) 78 );
      jht.title ( " " );
         int i, j, x = 0, y = 0, k = 0;
         char [ ] oc = new char [ 50 ];
         char [ ] busca = new char [ 2 ];
         char [ ] bus_ca = new char [ 2 ];
         String [ ] [ ] texto = {
                   {"No Meio do Caminho                        \n"},
                   {"Carlos Drummond de Andrade                \n"},
                   {"No meio do caminho tinha uma pedra        \n"},
                   {"Tinha uma pedra no meio do caminho        \n"},
                   {"Tinha uma pedra                           \n"},
                   {"No meio do caminho tinha uma pedra.       \n"},
                   {"Nunca me esquecerei desse acontecimento   \n"},
                   {"Na vida de minhas retinas tão fatigadas.  \n"},
                   {"Nunca me esquecerei que no meio do caminho\n"},
                   {"Tinha uma pedra                           \n"},
                   {"Tinha uma pedra no meio do caminho        \n"},
                   {"No meio do caminho tinha uma pedra.       \n"}};
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 10, ( short ) 2 );
         System.out.printf ( "PESQUISANDO E SUBSTITUINDO OCORRÊNCIAS"
              + " DE CARACTERES EM ARRAY " );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( short ) 29, ( short ) 4 );
         System.out.printf ( "Imprimindo abaixo o array " );
         char [ ] [ ] ch_ar = new char [ texto.length ] [ ];
         for ( i = 0; i < texto.length; i++ ) {
              jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
              for ( j = 0; j < texto [ i ].length; j++ ) {
                   ch_ar [ i ] = texto [ i ] [ j ].toCharArray ( );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.print ( ch_ar [ i ] );
              }
         }
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 24, ( int ) 19 );
         System.out.printf ( "Digite uma caracter: " );
         jht.textcolor ( jht.LIGHTRED );
         Scanner sc = new Scanner ( System.in );
         String c = sc.next ( );
         busca = c.toCharArray ( );// convert string para char
         /////////////////////////////////////////////////////////////////////
         for ( i = 0; i < ch_ar.length; i++ ) {
              jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
              for ( j = 0; j < ch_ar [ i ].length; j++ ) {
                   if ( ch_ar [ i ] [ j ] == busca [ x ] ) {
                      
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.print ( ch_ar [ i ] [ j ] );
                        oc [ y ] = ch_ar [ i ] [ j ];
                        y++;
                   } else {
                        jht.textcolor ( jht.LIGHTBLUE );
                        System.out.print ( ch_ar [ i ] [ j ] );
                   }
              }
         }
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 24, ( int ) 20 );
         jht.Sleep ( ( short ) 500 );
         System.out.printf ( "Encontramos " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", y );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " ocorrências de " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.print ( busca );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " no array " );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 30, ( int ) 22 );
         System.out.printf ( "PRESSIONE QUALQUER TECLA" );
         jht.getche ( );
         jht.Apaga ( ( short ) 19, ( short ) 20, ( short ) 20, ( short ) 68 );
         jht.Apaga ( ( short ) 20, ( short ) 21, ( short ) 14, ( short ) 75 );
         jht.Apaga ( ( short ) 22, ( short ) 23, ( short ) 25, ( short ) 60 );
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 24, ( int ) 19 );
         System.out.printf ( "Digite uma caracter para substituir: " );
         jht.textcolor ( jht.LIGHTRED );
         Scanner s_c = new Scanner ( System.in );
         String c_ = sc.next ( );
         bus_ca = c_.toCharArray ( );// convert string para char
         /////////////////////////////////////////////////////////////////////
         for ( i = 0; i < ch_ar.length; i++ ) {
              jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
              for ( j = 0; j < ch_ar [ i ].length; j++ ) {
                   if ( ch_ar [ i ] [ j ] == busca [ x ] ) {
                      ch_ar [ i ] [ j ] = bus_ca [ x ];
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.print ( ch_ar [ i ] [ j ] );
                        oc [ k ] = ch_ar [ i ] [ j ];
                        k++;
                   } else {
                        jht.textcolor ( jht.LIGHTBLUE );
                        System.out.print ( ch_ar [ i ] [ j ] );
                   }
              }
         }
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 24, ( int ) 20 );
         jht.Sleep ( ( short ) 500 );
         System.out.printf ( "Substituimos " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", k );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " ocorrências de " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.print ( busca );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " por " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.print ( bus_ca );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " no array " );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 30, ( int ) 22 );
         System.out.printf ( "PRESSIONE QUALQUER TECLA" );
         jht.getche ( );
         /////////////////////////////////////////////////////////////////////
         jht.Apaga ( ( short ) 19, ( short ) 20, ( short ) 20, ( short ) 68 );
         jht.Apaga ( ( short ) 20, ( short ) 21, ( short ) 14, ( short ) 70 );
         jht.Apaga ( ( short ) 22, ( short ) 23, ( short ) 25, ( short ) 60 );
         jht.Sleep ( ( short ) 1800 );
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 24, ( int ) 19 );
         System.out.printf ( "Por: " );
         jht.textcolor ( jht.LIGHTMAGENTA );
         System.out.printf ( "Samuel Lima" );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 24, ( int ) 20 );
         System.out.printf ( "sa_sp10@hotmail.com" );
         jht.Sleep ( ( short ) 1800 );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 34, ( int ) 22 );
         System.out.printf ( "MUITO OBRIGADO" );
         jht.getche ( );
         System.exit ( 0 );
         return 0;
     }
     public static void main ( String [ ] args ) {
         Pesq ( );
     }
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.