• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Probelm with equals method

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm new in Java and I have a problem with the equals method. I have the following code:

boolean t=false;
t=bot.rotulo.equals("Configuraci�n");
System.out.println(bot.rotulo);
System.out.println(t);
if(bot.rotulo.equals("Configuraci�n"))
{
RecordStore rs=null;
String[] cdatos={"ak","gj"};
int edatos[]=new int[2];
byte datos[]=new byte[2];


cdatos[0]="aksdkfjr23445jdkls";
cdatos[1]="gjh5tf567jjdgop989";

try
{
rs = RecordStore.openRecordStore("bastidores",true);
System.out.println("record store bastidores opened.");
}
catch(Exception e)
{
System.out.println("Error: "+e.getMessage());
}
finally
{
try
{
rs.closeRecordStore();
System.out.println("record store bastidores closed");
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage());
}
}
try
{
rs = RecordStore.openRecordStore("bastidores",true);
rs.addRecord(datos,0,datos.length);
if(rs.getNumRecords()>0)
{
for(int i=0;i<=datos.length;i++)
{
datos=cdatos[i].getBytes();
rs.addRecord(datos,0,datos.length);
System.out.println(datos[i]);
}
}

}
catch(Exception e){}
finally
{
try
{
rs.closeRecordStore();
}
catch (Exception e){}
}
}

The problem is that the t value is false when I select Configuraci�n button. That is, I have three buttons in the screen that are Configuraci�n, Recuento and Sincronizaci�n. When I click in some of this buttons I always obtain the same result: false and the if code never is done.


Anyone knows why?

I also tried using the CompareTo method but the result is the same.

Thanks a lot.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope I haven't misunderstood what you are trying to do, but from it appears that you are comparing a Button/JButton instance to a string literal. Is the name of your Button/JButton the same as the string ?
if so try something along the the lines of the following code snipppet

[ October 05, 2004: Message edited by: Nigel Browne ]
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying but the button is not a JButton as you say Nigel, because I work with MIDP 1.0 (J2ME) and with this profile button doesn't exists.

I have a method that returns button name call getRotulo() and the result is saved in a String. I just changed some code lines and now I have the following:

String rotulo = bot.getRotulo(); //I obtain button's name here
boolean t=false;

t=rotulo.equals("Configuraci�n");
System.out.println(rotulo);
System.out.println(t);

if(rotulo.equals("Configuraci�n"))
{
RecordStore rs=null;
String[] cdatos={"ak","gj"};
//String cdatos[];
int edatos[]=new int[2];
byte datos[]=new byte[2];

cdatos[0]="aksdkfjr23445jdkls";
cdatos[1]="gjh5tf567jjdgop989";

try
{
rs = RecordStore.openRecordStore("bastidores",true);
System.out.println("record store bastidores opened.");
}
catch(Exception e)
{
System.out.println("Error: "+e.getMessage());
}
finally
{
try
{
rs.closeRecordStore();
System.out.println("record store bastidores closed");
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage());
}
}
try
{
rs = RecordStore.openRecordStore("bastidores",true);
rs.addRecord(datos,0,datos.length);
if(rs.getNumRecords()>0)
{
for(int i=0;i<=datos.length;i++)
{
datos=cdatos[i].getBytes();
rs.addRecord(datos,0,datos.length);
System.out.println(datos[i]);
}
}

}
catch(Exception e){}
finally
{
try
{
rs.closeRecordStore();
}
catch (Exception e){}
}
}

After running this code I have the same problem.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that java is case sensitive. The following code will produce a result of true and the false due to the Capitol 'T' in the second test

[ October 05, 2004: Message edited by: Nigel Browne ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output from these lines of code:

Mostly, I am interested in what the second line above prints out.

In regard to the above comment about case-sensitivity, you can use String.toUpper() or String.toLower() in order to ignore case, if you wish.

Also, for future reference, you should use CODE tags when you post code. They will preserve formatting so your code will be easier to read. There are nice little buttons just below the message editing text area to help you.

Keep coding!

Layne
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In regard to the above comment about case-sensitivity, you can use String.toUpper() or String.toLower() in order to ignore case, if you wish.



You could also use the equalsIgnoreCase(String anotherString) method of the String class
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Layne, first line prints out Configuraci�n, the same as I have typed as button name, and the second line prints out false.

I know Java is case sensitive but what I type as a name button is exactly the same as I type in equals method.

I don't know what happens but I can't solve the problem.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is very strange indeed. My first suspicion is that there may be some minor typo in one of the two Strings. Typically when you read, you don't look at each character in the words. In fact, a good reader rarely concentrates on individual words, so it's easy for the eye to skim over a word and not catch a misspelling. Have you checked each letter one by one to make sure the word is spelled the same in both spots?

A possible solution is to create a String constant that can be used when you create the button and later reused for comparison. This will greatly decrease the chance for an error caused by simple misspelling.

HTH

Layne
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible that the "Configuraci�n" returned by bot.rotulo has extra whitespace at the beginning or end?

Perhaps you should write a method which compares two strings by looping through each character position and checking to see if the characters in each string are equal. If not, you can report exactly which characters are different. (Maybe there's more than one way to represent the '�' in your system?)
 
Alex Mun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeees! That was the problem, the spaces at the end of Configuraci�n. I compared "Configuraci�n" to "Configuraci�n " and that's why it appears false in the comparison.

I'm new in Java and after reading Java's API I were not able to know that equals method compares two Strings exactly including spaces.

Thanks to everybody who has replied to me, especially Jim.
 
Hey cool! They got a blimp! But I have a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic