Como comparar um char em um array java

  • hi,see you r using "==" which checks equality of reference variables in java, so use equal() method of java.lang.object which makes value check so use it as a.equals(a[j]) this will work .it is java be care full while using "==" and equals().

  • I am not actually getting what you exactly want to do. Looking at the C++ example, it seems there are some errors like in " if(a==a[j]){ ", "a" has to be variable which I do not see. I might be wrong but I am not getting what exactly you want ?

  • HI ,I have given the code to compare 2 chars are equalimport java.util.Arrays;public class CompareCharArraysExample { public static void main(String[] args) { //create character arrays char[] charArray1 = new char[]{'d','h','r','f'}; char[] charArray2 = new char[]{'d','h','r','f'}; boolean blnResult = Arrays.equals(charArray1,charArray2); System.out.println("Are two char arrays equal ? : " + blnResult);// Note : put the logic as under: i) if blnResult gives ' true' value, do some logic. ii) if blnResult gives ' false' value, do some other logic.//pls proceed according to ur requirement }}Regards,Katchapakesan.T

  • public class Chek {public static void main(String args[]) {String a[]={"A","B","C"};String[] b={"B","B","D"};for(int i=0;i{for( int j=0;j{if(a.equals(b[j])){System.out.println("Matched Charecter in Array a["+i+"]"+a);System.out.println("Matched Charecter in Array b["+j+"]"+b[j]);}}}}}use this code to compare your code solve the problem

  • Hi, thank you for your response.Here is the description of my codethe main purpose of this code is,1) This program will read two names from keyboard into an array.2) Now it will compare each and every character between two arrays, and repalces with an empty space.3) At final it will count the remaining characters in both arrays,I completed in c++ but in java/jsp I am not getting issueschar a[20],b[20];int size,size1,i,j;cout

  • Hi,Run this code. Hope it will resolve your problem and you can understand howhow it is happeningpublic class Chek {public static void main(String args[]) {String a[]={"A","B","C"};String b[]={"B","B","D"};for(int i=0;i{for( int j=0;j{if(a.equals(b[j])){System.out.println("Matched Charecter in Arraya["+i+"]"+a);System.out.println("Matched Charecter in Arrayb["+j+"]"+b[j]);}}}}}

:roll: Gostaria de saber se é possível comparar o conteúdo de um array do tipo char? Utilizei o método equals e não funcionou, onde será que estou errando?

Exemplo: char[] array = new char[100];
if (array[i].equals(";"))

Beijos - Sheyla_DF :wink:

Tenta usar com aspas simples:

if (array[i].equals(';'))

Ou talvez vc tenha q usar objeto tipo:

Character[] array = new Character[100];

Acho q é isso 8O

Como comparar um char em um array java

[quote=“Sheyla_DF”]:roll: Gostaria de saber se é possível comparar o conteúdo de um array do tipo char? Utilizei o método equals e não funcionou, onde será que estou errando?

Exemplo: char[] array = new char[100];
if (array[i].equals(";"))

Beijos - Sheyla_DF :wink:[/quote]

Não existe equals em char, é um tipo primitivo e se compara usando ==

if(array[i] == ‘;’)

PS: note que usei aspas simples, e nao aspa duplas, aspa simples = char…aspa dupla = string.

  Compare Two Java char Arrays Example

  This java example shows how to compare two char arrays for equality using

public class CompareCharArraysExample {

  public static void main(String[] args) {

    //create character arrays

    char[] charArray1 = new char[]{'d','h','r','f'};

    char[] charArray2 = new char[]{'d','h','r','f'};

      To compare two char arrays use,

      static boolean equals(char array1[], char array2[]) method of Arrays class.

      It returns true if both arrays are equal. Arrays are considered as equal

      if they contain same elements in same order.

    boolean blnResult = Arrays.equals(charArray1,charArray2);

    System.out.println("Are two char arrays equal ? : " + blnResult);

      Please note that two char array references pointing to null are

Output of the program would be

Are two char arrays equal ? : true