This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes io/string question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "io/string question" Watch "io/string question" New topic
Author

io/string question

patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
hi all, i have a question about io

method findStore(String s) is supposed to be able to find the store with the same name as parameter string s from a store list under the mall directory. however, it doesnt work properly. i try to create 2 stores and use findStore("store2") to search for store2, but it wont return store2, even string s1 and s2 are exactly the same.
i also checked documentation of String's equals method as follows,

equals
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only
if the argument is not null and is a String object that represents the same
sequence of characters as this object.
Overrides:
equals in class Object
Parameters:
anObject - the object to compare this String against.
Returns:
true if the String are equal; false otherwise.

it looks like i'm using this method properly.but i just dont get the good result. can anybody pls help me out...again? thanks.
[ February 19, 2002: Message edited by: patrick tang ]
[ February 19, 2002: Message edited by: patrick tang ]
[ February 19, 2002: Message edited by: patrick tang ]
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
Patrick,
It would appear that you are finding the right store but you are returning the wrong variable. In the line below, you declare the variable findstore. However, in the method findStore, the only variable that could tell you if there is a match is filestore. Basically, I think you meant to return filestore instead of findstore.


[ February 19, 2002: Message edited by: Rodney Woodruff ]

Hope This Helps
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Are you sure you posted the correct version of the code. This one doesn't even compile.
When you are asking a question about the output of some code, make sure the code compiles at least, before posting it here.
If you are having difficulty getting some code to compile then phrase your question accordingly.
You are more likely to get help, when your code compiles and some one can run it easily .
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
hi rodney, thanks for pointing out that
error. it's actually a typo. sorry about
that. i'll fix it soon.
i've actually compiled and run the program
couple of times. i've put a line:
System.out.println("yes") in if(s1.equals(s2))
block. it doesnt print yes.
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
When you compile and run the code what output is produced by these lines

System.out.println(s1); //1
System.out.println(s2); //2


That should be your clue to why the equals() method is returning false.
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
thats why i'm confused.
as there're 2 stores in the mall, in for block,
1st loop:
s1=mall\store2
s2=mall\store1
2nd loop:
s1=mall\store2
s2=mall\store2
you can see s1 and s2 are the same strings but it wont print yes and return store2.
flynn xi
Greenhorn

Joined: Jan 14, 2002
Posts: 8
Your printout should be:
loop1:
mall\store2
store1
loop2:
mall\store2
store2
This is why no "yes" printed out. Try getPath(), rather than getName(). Hope it is helpful.
BTW, your code is really to be modified to be compiled correctly. I know it is taken out of a project in a hurry.
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
s1 mall\store2
s2 store1
s1 mall\store2
s2 store2
the result is null
This is what I get when I run the program.
However if I change the line from
//String s1 = new String("mall"+File.separator+s);
To
String s1 = s ;
then the program works fine.
I am eunning JDK 1.3.1 on Win nT platform
I suggest you read the specs on the getName method carefully once again.
HTH
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
thanks shivaji and flynn for your prompt reply.
i appreciate it.
i'll try to use getPath instead of getName and
hopefully this will work fine.
btw, i just went to api documentation and read two methods again,

public String getName()
Returns the name of the file or directory denoted by this abstract
pathname. This is just the last name in the pathname's name sequence.
If the pathname's name sequence is empty, then the empty string is
returned.
Returns:
The name of the file or directory denoted by this abstract pathname,
or the empty string if this pathname's name sequence is empty


public String getPath()
Converts this abstract pathname into a pathname string. The resulting
string uses the default name-separator character to separate the names in the
name sequence.
Returns:
The string form of this abstract pathname

will getPath return a "dir/file"-like string? coz
i cant try it now. i remember it just returns a string which is the filename. anyway,i can try it later on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: io/string question
 
Similar Threads
write to a file in another class
Need Help in Sorting Multiple columns!
file/io question during practising
about "=="and "equals" problem,I can't understand clearely?
io question