aspose file tools*
The moose likes Beginning Java and the fly likes String replace method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replace method problem" Watch "String replace method problem" New topic
Author

String replace method problem

Jeff Pavlocak
Greenhorn

Joined: Apr 26, 2004
Posts: 16
I'm having a problem getting the String classes replace method to work.

I'm using the the J2SE 1.5.0 SDK and the API spec states that the String class now has an overloaded replace(CharSequence target, CharSequence replacement) method.

When I use this method, I can't get the program to compile. Here's a sample:

public class TestReplace
{
public static void main(String[] args)
{
String s = "aaa";
CharSequence csTarget = "aa";
CharSequence csReplace = "b";
String s2 = s.replace(csTarget, csReplace);
System.out.println(s2);
}
}

Compiling this yields the following error:

C:\dev\src>javac TestReplace.java
TestReplace.java:9: replace(char,char) in java.lang.String cannot be applied to
(java.lang.CharSequence,java.lang.CharSequence)
String s2 = s.replace(csTarget, csReplace);
^
1 error

My CLASSPATH is: c:\jdk1.5.0;.

Any ideas??

Jeff
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Your code works fine. How is your PATH set ? yoou must be compiling with a previous version of the jdk.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Nigel comments, the problem is with your PATH variable. You need to set

CLASSPATH is a completely different creature. It is only used by Java internally to find .class files. PATH on the other hand is used by the operating system to locate executable programs. The later is the issue here since the OS needs to find the javac.exe program to compile your code.

HTH

Layne


Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Layne Lund:




That would be "jdk1.5.0\bin", of course, and, whereas the PATH likely needn't hold ".", there are lots of other things it needs to hold, to.

But you do need to confirm that, if you've got both JDK1.4 and 1.5 installed on your system, that either 1.5 comes first on your PATH, or you invoke the 1.5 compiler explicitly by using the full path.

It's possible, actually, that you've put jdk1.4\jre\lib\rt.jar into your CLASSPATH, though, and JDK 1.5 is using it. My advice to you is that unless you really understand CLASSPATH, the best thing to do is to ensure that it's not set to anything at all.


[Jess in Action][AskingGoodQuestions]
Jeff Pavlocak
Greenhorn

Joined: Apr 26, 2004
Posts: 16
I got it to work.

I did have a previous version of Java installed in another set of directories. I completely removed those directories and everything works fine now.

Thanks for the help.

Jeff
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String replace method problem