Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Carcleo

Comparando valores do array

Recommended Posts

Estou com o seguinte problema

 

 

class RemoveRepetidas {
 
  String[] cadaLinha = null;
 
  RemoveRepetidas (String[] _cadaLinha) {
    
this.cadaLinha = _cadaLinha; 
 
  }  
  
  int [] listaIndicesARemover (int _j) {
    
int [] indicesARemover = null;
 
int contador = 0;
 
for ( int i = _j; i < this.cadaLinha.length; i++ ) {
 
      System.out.println ( this.cadaLinha[i] + " - " + this.cadaLinha[ i + 1 ] );  //linha A
 
 if ( this.cadaLinha[i] == this.cadaLinha[ i + 1 ] ) {   //linha A
 
        System.out.println ( this.cadaLinha[i] + " - " + this.cadaLinha[ i + 1 ] );  
 
   this.listaIndicesARemover( i + 1 ); 
 
indicesARemover[contador] = i + 1;
 
   contador++;
      } 
 
    
     if (i + 1 == this.cadaLinha.length - 1) { 
 
break; 
 
 }
 
}
 
return indicesARemover;
 
  }
  
}
Na linha A, se eu pedir para imprimir por exemplo:
System.out.println ( this.cadaLinha[3] + " - " + this.cadaLinha[4] ); 
Ele imprime
texto - texto
Mas quando mando comparar na linha B se texto é igual a texto ele esta dizendo que não é igual.
 if ( this.cadaLinha[3] == this.cadaLinha[ 4] ) { .......
Onde estou errando?

Consegui fazer a comparação, mas é preciso fazer uma recursão à cada vez que existe a repetição.

Mas não estou encontrando a lógica.

 

class RemoveRepetidas {
 
  String[] cadaLinha = null;
 
  RemoveRepetidas (String[] _cadaLinha) {
    
this.cadaLinha = _cadaLinha; 
 
  }  
  
  int [] listaIndicesARemover (int _j) {
    
int [] indicesARemover = null;
 
int contador = 0;
 
for ( int i = _j; i < this.cadaLinha.length - 1; i++ ) {
      
 if ( this.cadaLinha[i].compareToIgnoreCase(this.cadaLinha[ i + 1 ]) == 0 ) {
 
        System.out.println ( this.cadaLinha[i] + " - " + this.cadaLinha[ i + 1 ] );   
 
indicesARemover[contador] = i + 1;
 
   contador++;
 
   this.listaIndicesARemover( i + 1 ); 
      } 
 
}
 
return indicesARemover;
 
  }
  
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Consegui.

 

import java.io.File;
 
import java.io.FileWriter;
import java.io.BufferedWriter;
 
import java.io.FileReader;
import java.io.BufferedReader;
 
import java.io.IOException;
 
import java.util.ArrayList;
 
class Principal {
  
  public static void main (String[] args) {
    
try {
 
 String arq = "arquivo.txt";
 
 FileReader fr = new FileReader (arq);
 BufferedReader br = new BufferedReader (fr);
 
 String linha = br.readLine ();
 
 String[] cadaLinha = null;
 
 while (linha != null) {
 
   cadaLinha = linha.split(" "); 
 
   linha = br.readLine ();
 
 }
   
 RemoveRepetidas rr = new RemoveRepetidas (cadaLinha);
 
 ArrayList<String> textoFinal = rr.listaPalavras();
 
      String pronta = "";
 
 for (int i = 0; i < textoFinal.size(); i++) {
       
pronta += textoFinal.get(i) + " ";
 
 } 
 
 System.out.println(pronta); 
 
}
catch (IOException e)
{
  System.out.println (e.getMessage());
}
 
  }
 
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.