Concatenation in Stringsurgent!!!
I have a function name in my jsp which is
concatenation of a string
String sBody=sName+Body();
the function name should be sName+Body
for eg
if sName="pprops";
then function name is
ppropsBody()
the first part of the functionname can vary ...i.e. sName value
Can anybody help me out from this..
if I give as above
sName+Body()
it gives exception saying NO Method Body found..
urgent pl
smita
[503 byte] By [
smitaunni] at [2007-9-26 3:31:41]

for string concatination u have to have 2 strings.in your case if u had sName+"Body()"; there won't be any probs..orsName.concat("Body");either way..I hope this helpsKhalid Ali
me_elf at 2007-6-29 11:58:27 >

I did try concat...but I am not getting the function return value
if i say
sBody=sName.concat("Body()");
sBody should be the return value of my function...
bit it gives me
if sName="pprops"
sBody=ppropsBody();
which is not what I want but the return value..
Is there anything else I can do
pl ...help
I think what you are trying to do is to call different methods base on the value of sName. If you know the values of all your sName's in advance, you can simply write a function that handles which sNameBody() function to call.
public String getyBodyBySname(String sname) {
if(sName.equals("a")) {
return aBody();
} else if(sName.equals("b")) {
return bBody();
} else {
return defaultBody();
}
// to get sBody
String sBody = getyBodyBySname(sName);
Not sure whether this is what you want...
Ya! This is what I want...and I knew it could be done so...But is there no one line solution for this like a eval function in javascript or ASPsbody=eval(string)pl....help...urgent
I have been through this a technical requirement like this one before.
And even I said to myself...
If only Java had the eval function like in Javascript.
But it does not. So I wrote my own implementation of eval(). My home-grown implementation of eval() is based on java.lang.Class.getDeclaredMethod()
You see, the first String parameter that you pass to Class.getDeclaredMethod(String methodName, ...) can be constructed like...
String methodName= "something" + "Body()";
You can try this
String name = "sprops";
java.lang.reflect.Method m = this.getClass().getMethod(name+ "Body", null);
m.invoke(this, null);
If there are any parameters that needs to be passes
m.invoke(this, parameter as Object[])
Hope this helps you
Thanks Sampath ..
I tried using java.lang.reflect...But it gives me an exception saying no such method found..
This is a dummy prog..
Pl if possible check this and correct me
<%@ page language="java"%>
<%@ page import="java.sql.*,java.net.*,java.text.*,java.util.*,javax.servlet.jsp.JspWriter,java.awt.*,org.w3c.dom.*,javax.xml.parsers.*,java.io.*,org.apache.xerces.parsers.*,org.xml.sax.*,javax.servlet.http.*,java.lang.reflect.*" %>
<%@ page session="true" %>
<%
String a[]=new String[5];
int i=0;
String sName="pprops menutop Scroller Static";
StringTokenizer st = new StringTokenizer(sName," ");
while (st.hasMoreTokens())
{
a=st.nextToken();
java.lang.reflect.Method m=this.getClass().getMethod(a+"DoThis",null);
String retVal= String .valueOf(m.invoke(this, null));
out.println("this is"+retVal);
i=i+1;
}
%>
<%! String ppropsDoThis()
{
return "pprops";
}
%>
<%! String menutopDoThis()
{
return "menutop";
}
%>
<%! String ScrollerDoThis()
{
return "Scroller";
}
%>
<%! String StaticDoThis()
{
return "Static";
}
%>
Pl tell is there anything wrong in the logic or syntax...if possible correct me..For the array...I have done the synatx properly...here somehow why I am not sure...the square brackects r not appearing...
Thanxs for ur support anyway.
smita
Class.getMethod only returns public method. So make those xxxxDoThis() methods public.
yilin at 2007-6-29 11:58:27 >

Also in your code, "a" should be String, not String[]; otherwise it won't even compile.String a[]=new String[5]; // <-- wrong.String a; // <-- correcta=st.nextToken();
yilin at 2007-6-29 11:58:27 >

If you call getDeclaredMethod() instead of getMethod(), then you don't need to make the other methods public because getDeclaredMethod() can return package-private methods also.Yi
yilin at 2007-6-29 11:58:27 >

As I said in my previous reply, java.lang.Class.getDeclaredMethod() is the one I used to implement my own eval().
smitaunni,
I'm no expert but I think I see what your problem is in your dummy code. It looks like you're commenting out your methods which may be why you're getting meethod not found errors. look at your code a minute:
<%! String ppropsDoThis()
{
return "pprops";
}
%>
<%! String menutopDoThis()
{
return "menutop";
}
%>
<%! String ScrollerDoThis()
{
return "Scroller";
}
%>
<%! String StaticDoThis()
{
return "Static";
}
%>
for each of these methods you're enclosing them with a <%! ... %> combination. Remove the ! and try again.
A readable re-post
smitaunni,
I'm no expert but I think I see what your problem is in
your dummy code. It looks like you're commenting out
your methods which may be why you're getting meethod
not found errors. look at your code a minute:
<%! String ppropsDoThis()
{
return "pprops";
}
%>
<%! String menutopDoThis()
{
return "menutop";
}
%>
<%! String ScrollerDoThis()
{
return "Scroller";
}
%>
<%! String StaticDoThis()
{
return "Static";
}
%>
for each of these methods you're enclosing them with a
<%! ... %> combination. Remove the ! and
try again.
If you make all the changes I mentioned, it works fine. I have already tested it!
yilin at 2007-7-1 9:06:25 >

Thanks a lot...Yilin...ur solution works fine..I have used a string instead of an array and used getDeclared method...which has solved the problem..
great...Thanks to all others also for contributing ur views and helping me out...hope I can come of some help to u later.
smita
You are welcome. You haven't assigned anyone your Duke dollars yet. People will be glad to be honored for their work, though it's not real money, but tokens of honor/appreciation.
yilin at 2007-7-1 9:06:25 >

Sorry...But i have assigned 2 Duke Dollars for this topic...if u check...
But now my problem is still not solved still...I am facing a problem in assigning paarameters to the function...can u send me a small code for assigning the parameters...I tried using object[] but still I get exception.
regarding the Duke Dollars pl clarify!
smita
Yes, you have assigned the Duke dollars to the question. But after the problem is solved, you need to assign that Duke dollars from the question to a particular user(s)/message(s), so that the Duke dollars will appear beside the message(s), no longer at the begining of the question anymore. (Check the top part of your page, there must be links for you to distribute the Duke dollars among users.)
When you call getDeclaredMethod(), you need to pass in parameter classes as Class array. Next, when you call invoke, pass parameter as an array of Objects. The following is an example from http://web2.java.sun.com/docs/books/tutorial/reflect/object/invoke.html. If you still have exceptions, please show what exception you get and your source code:
import java.lang.reflect.*;
class SampleInvoke {
public static void main(String[] args) {
String firstWord = "Hello ";
String secondWord = "everybody.";
String bothWords = append(firstWord, secondWord);
System.out.println(bothWords);
}
public static String append(String firstWord, String secondWord) {
String result = null;
Class c = String.class;
Class[] parameterTypes = new Class[] {String.class};
Method concatMethod;
Object[] arguments = new Object[] {secondWord};
try {
concatMethod = c.getMethod("concat", parameterTypes);
result = (String) concatMethod.invoke(firstWord, arguments);
} catch (NoSuchMethodException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (InvocationTargetException e) {
System.out.println(e);
}
return result;
}
}
yilin at 2007-7-1 9:06:26 >

thanxs again for ur response...on the top of the page...I can see just a message saying there are 2 duke dollars aasigned for this page...But no link....Can u help me out...smita
thanxs again for ur response...on the top of the page...I can see just a message saying there are 2 duke dollars aasigned for this page...But no link....Can u help me out...smita
I have sorted out the Duke Dollars if u check
I am pasting my code below...It is giving me NoSuchMethodException...Can u point out the error
<%
String a[]=new String[5];
String a1="";
int i=0;
int j=0;
String sName="pprops menutop Scroller Static";
String secondWord = "everybody.";
StringTokenizer st = new StringTokenizer(sName," ");
StringTokenizer stt = new StringTokenizer(sName," ");
Class c=String.class;
Class[] parameterTypes = new Class[] {String.class};
Object[] arguments = new Object[] {sName,secondWord};
while (st.hasMoreTokens())
{
a=st.nextToken();
a1=a;
java.lang.reflect.Method m=this.getClass().getDeclaredMethod(a1+"DoThis", parameterTypes);
String retVal=String.valueOf(m.invoke(this, arguments));
//out.println("this is"+retVal);
i=i+1;
}
%>
<%! String ppropsDoThis(String nv,String sec)
{
return "ppp"+nv;
}
%>
<%! String menutopDoThis(String nv,String sec)
{
return "mtt";
}
%>
<%! String ScrollerDoThis(String nv,String sec)
{
return "sc";
}
%>
<%! String StaticDoThis(String nv,String sec)
{
return "st";
}
%>
Change the following line before the while loop to:Class[] parameterTypes = new Class[] {String.class, String.class}; // since you have 2 parameters, both are of class String( or you also use your variable "c": Class[] parameterTypes = new Class[] {c, c};)
yilin at 2007-7-1 9:06:26 >

Hi I was one to post about using java.lang.reflect.Method well no Duke for me , just kidding well its nice to see that ur problem is sloved. Thanks all for responses for her question.ThanksSampath
Sorry...since yilin was the one who gave me the code for my problem I just assigned it ...Sorry again...Hope I can work it sometime again..Anyway thanks for ur response.smita