-
String Replacement
Hi, I'm going to start to learn Java outside of college. Learning Java in making own programs etc. Not scholar programs.
But I have a problem in getting text back. What do I need to add to this text?
package ontsleuteling1;
public class Replace
{
public static String replacement(String args[])
{
String str="ryhhf,MleiDffkwstkGyyoFfwTloIyykFbols2.Pyykfm RsuLwGlzosksiFihlisdlyOfkSsiKswsiligGsfbsip.RsoItm mskLz978.245.823.456Gkfso,Gfivf";
str.replaceAll( "F","O" );
str.replaceAll( "f","o" );
str.replaceAll( "G","G" );
str.replaceAll( "g","g" );
str.replaceAll( "H","L" );
str.replaceAll( "h","l" );
str.replaceAll( "I","N" );
str.replaceAll( "i","n" );
str.replaceAll( "R","H" );
str.replaceAll( "r","h" );
str.replaceAll( "V","Z" );
str.replaceAll( "v","z" );
str.replaceAll( "Y","A" );
str.replaceAll( "y","a" );
return str;
// System.out.print(str); --> doesn't work, why not?
}
}
-
replaceAll method returns another string. So your code should look like
str=str.replaceAll( "F","O" );
-
okay, thanks. I replaced. Could you tell me why I can't put the System.out.println("str"); there?
-
You have a return statement before that, so the flow control will never reach to that line. You may put that print statement before return.
-
Console : Exception in thread "main" java.lang.NoSuchMethodError: main
-
Have you coded the main function in your class?
-
it works now :)
package ontsleuteling1;
public class Replace
{
public static void main(String args[])
{
String str="ryhhf,MleiDffkwstkGyyoFfwTloIyykFbols2.Pyykfm RsuLwGlzosksiFihlisdlyOfkSsiKswsiligGsfbsip.RsoItm mskLz978.245.823.456Gkfso,Gfivf";
str = str.replaceAll( "F","O" );
str = str.replaceAll( "f","o" );
str = str.replaceAll( "G","G" );
str = str.replaceAll( "g","g" );
str = str.replaceAll( "H","L" );
str = str.replaceAll( "h","l" );
str = str.replaceAll( "I","N" );
str = str.replaceAll( "i","n" );
str = str.replaceAll( "R","H" );
str = str.replaceAll( "r","h" );
str = str.replaceAll( "V","Z" );
str = str.replaceAll( "v","z" );
str = str.replaceAll( "Y","A" );
str = str.replaceAll( "y","a" );
System.out.println( str + "");
}
}
-
okay, I want to translate that sentence. But it doesn't work proper on this way.
Do you know how to add this in an array or something? I think that that must help.
Im not sure.