File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Probelm with equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Probelm with equals method" Watch "Probelm with equals method" New topic
Author

Probelm with equals method

Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
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.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Oct 05, 2004
Posts: 14
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

Joined: May 15, 2001
Posts: 673
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 ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

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

Joined: Oct 05, 2004
Posts: 14
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

Joined: Dec 06, 2001
Posts: 3061
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?)


"I'm not back." - Bill Harding, Twister
Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
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.
 
Consider Paul's rocket mass heater.
 
subject: Probelm with equals method