quarta-feira, 12 de abril de 2017

Pesquisando e substituindo ocorrências de strings em array

Para finalizar este assunto mostro agora
um bom exemplo de como substituir string
dentro de um array multidimensional.
Isto também foi feito usando array de char
e achei muito útil também fazê-lo com string.

Veja abaixo uma imagem 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, l = 0;
         String [ ] oc = new String [ 50 ];
         String [ ] busca = new String [ 2 ];
         String [ ] bus_ca = new String [ 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 ) 12, ( short ) 2 );
         System.out.printf ( "PESQUISANDO E SUBSTITUINDO OCORRÊNCIAS"
               + " DE STRINGS EM ARRAY " );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( short ) 29, ( short ) 4 );
         System.out.printf ( "Imprimindo abaixo o array " );
         for ( i = 0; i < texto.length; i++ ) {
         jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
         for ( j = 0; j < texto [ i ].length; j++ ) {
               jht.textcolor ( jht.LIGHTBLUE );
               System.out.print ( texto [ i ] [ j ] + " " );
         }
         }
         /////////////////////////////////////////////////////////////////////
         //A coluna esquerda da moldura está sendo
         //refeita através deste método apaga
         jht.textbackground ( jht.BLACK );
         jht.Apaga ( ( short ) 4, ( short ) 26, ( short ) 0,( short ) 2 );
         jht.textbackground ( jht.WHITE );
         /////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 24, ( int ) 19 );
         System.out.printf ( "Digite uma Palavra: " );
         jht.textcolor ( jht.LIGHTRED );
         Scanner dados = new Scanner ( System.in );
         busca [ k ] = dados.nextLine ( );
         ////////////////////////////////////////////////////////////////////
         for ( i = 0; i < texto.length; i++ ) {
         jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
         for ( j = 0; j < texto [ i ].length; j++ ) {
               if ( texto [ i ] [ j ].contains ( busca [ k ] ) ) {
                    jht.textcolor ( jht.LIGHTRED );
                    System.out.print ( texto [ i ] [ j ] + " " );
                    oc [ y ] = texto [ i ] [ j ];
                    y++;
               } else {
                    jht.textcolor ( jht.LIGHTBLUE );
                    System.out.print ( texto [ 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 [ k ] );
         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" );
         /////////////////////////////////////////////////////////////////////
         //A coluna esquerda da moldura está sendo
         //refeita através deste método apaga
         jht.textbackground ( jht.BLACK );
         jht.Apaga ( ( short ) 4, ( short ) 26, ( short ) 0,( short ) 2 );
         jht.textbackground ( jht.WHITE );
         /////////////////////////////////////////////////////////////////////
         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 Palavra para substituir " );
         jht.textcolor ( jht.LIGHTRED );
         Scanner da_dos = new Scanner ( System.in );
         bus_ca [ l ] = da_dos.nextLine ( );
         ////////////////////////////////////////////////////////////////////
         k = 0;
         for ( i = 0; i < texto.length; i++ ) {
         jht.gotoxy ( ( short ) 24, ( short ) i + 6 );
         for ( j = 0; j < texto [ i ].length; j++ ) {
               if ( texto [ i ] [ j ].contains ( busca [ k ] ) ) {
                    texto [ i ] [ j ] = bus_ca [ l ];
                    jht.textcolor ( jht.LIGHTRED );
                    System.out.print ( texto [ i ] [ j ] + " " );
                    oc [ x ] = texto [ i ] [ j ];
                    x++;
               } else {
                    jht.textcolor ( jht.LIGHTBLUE );
                    System.out.print ( texto [ i ] [ j ] + " " );
               }
         }
         }
         /////////////////////////////////////////////////////////////////////
         //A coluna esquerda da moldura está sendo
         //refeita através deste método apaga
         jht.textbackground ( jht.BLACK );
         jht.Apaga ( ( short ) 4, ( short ) 26, ( short ) 0,( short ) 2 );
         jht.textbackground ( jht.WHITE );
         /////////////////////////////////////////////////////////////////////
         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", x );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " ocorrências de " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.print ( busca [ k ] );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( " por " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.print ( bus_ca [ l ] );
         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.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.