Solved

Why this not working !!!

Posted on 2004-09-08
6
221 Views
Last Modified: 2010-03-31
Hello,

Question::
method that work...........
String str=object.getAccountId().trim(); //this question is on the line
if(str.equals("12345"))
{
  System.out.print("12345");
}
else
{
 System.out.print("Error");
}

method that doesn't work......
if(object.getAccountId().trim().equals("12345")) // this line does not work why this happen????
 System.out.print("12345");
else
 System.out.print("error");

Thk U
From newbie
0
Comment
Question by:nStudent
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12005298
shouldn't be any difference
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12005306
>> this line does not work
You mean?
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 12005315
There's no difference between those two examples.
Second should work too, perhaps the accountId was not 12345 when you tested the second one? ;)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Accepted Solution

by:
corduroy9 earned 50 total points
ID: 12005475
you may have to cast it as a string before the trim, like...

if(  ((String)(object.getAccountId())).trim().equals("12345"))
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 12005481
or before the equals()
0
 
LVL 4

Expert Comment

by:sgalzin
ID: 12005506
hi,

depending on what getAccountId returns :

/*** returns object : ***/
if (object.getAccountId().toString().trim().equals("12345"))
 System.out.print("12345");
else
 System.out.print("error");


/*** returns int or similar : ***/
if ((new Integer(object.getAccountId())).toString().trim().equals("12345"))
 System.out.print("12345");
else
 System.out.print("error");


is any of these better ?

stephane.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now