Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replace method problem

 
Jeff Pavlocak
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works fine. How is your PATH set ? yoou must be compiling with a previous version of the jdk.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Pavlocak
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic