• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

io/string question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic