Hi, I'm trying to use the replace() method to take out all instances of " "
, (space) with an underscore, ("_"). I'm doing this because I'm using URLs to connect to a servlet. I keep getting this error:

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
        url.replace(" ", "_");

This is how I wrote my code:

url = url.replace(" ", "_");

What am I doing wrong? Any suggestions would be most welcome.

Thanks,
chuck

Try replaceAll() instead.

I'm getting this error now:

cannot find symbol
symbol  : method replaceAll(java.lang.String,java.lang.String)
location: class java.lang.String
        url = url.replaceAll(" ", "_");

That's just confusing. :confused: Any other suggestions?

It works for me.

Is url a String?

I think you forgot to change URL to string, so you can't apply String method replaceAll() to url
This can help you

import java.net.URL;
import java.net.MalformedURLException;

public class ReplaceChar
{
	public static void main(String[] args)
	{
		try
		{			
			URL url = new URL("http://www.peter-peter.pe");
			url = new URL(url.toString().replaceAll("e", "i"));
			System.out.println(url.toString());
		}
		catch(MalformedURLException mue)
		{
			mue.printStackTrace();
		}
	}
}

Hi, I'm trying to use the replace() method to take out all instances of " "
, (space) with an underscore, ("_"). I'm doing this because I'm using URLs to connect to a servlet. I keep getting this error:

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)

url.replace(" ", "_");

This is how I wrote my code:

url = url.replace(" ", "_");

What am I doing wrong? Any suggestions would be most welcome.

Thanks,
chuck

Do you notice something about the highlighted protions of the error message?

change url.replace(" ", "_"); to url.replace(' ', '_');

I have it. replaceAll is not in the Java ME api. I made a rather stupid assumption :mad: and wasted your time. I've fixed the problem . Many thanks for your time and efford. ;)
chuck

Next time please let us know that you using different API

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

No it doesn't, and besides, as of Java 5 the way he tried it in the OP will work, but since this thread is a four year dead zombie, that doesn't matter anymore, and neither does the post I'm responding to. Killing this zombie now.

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

Bad advice exactly showing that you did not read whole thread otherwise you would have seen that original poster then said that Java Microedition API was in use. Unlike J2SE JME has limited classes and that also mean less methods to use.

the replace method cannot be applied to Strings, only to chars

String s="MXLXYXLXM";
String p=s.replace(3,'X','A');
In compiling it shows an error that method(int,char,char) not found.please anyone can explain me this I am really troubled.

what you could have done is start your own thread, since this is:
a. not the same issue
b. a 6 year old dead thread, that shouldn't have been revived.

what on earth is that int doing in that method call?
anyway, check this to find out what options you have for that method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.