Solved

Why this not working !!!

Posted on 2004-09-08
6
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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