jaggernat
asked on
reading elements from string array
Dear experts
I have this little peice of code on which i want clarification
bean contains a field
private String[] allLRNs = new String[0]; and valueobject also contains the same field
private String[] allLRNs = new String[0];
I am setting values in the string array(in bean) and setting it to value object.
Later on i want to get the array values from the valueobject and set it to bean by doing this
bean.setAllLRNs(incidentNo tification VALUEOBJ.g etAllLRNs( ));
System.out.println("IDEN VALUE" +incidentNotificationVO.ge tAllLRNs() );
BUT incidentNotificationVO.get AllLRNs() prints/gives me the Array object. HOW do i get to see the
individual string elements in it?
thanks
J
I have this little peice of code on which i want clarification
bean contains a field
private String[] allLRNs = new String[0]; and valueobject also contains the same field
private String[] allLRNs = new String[0];
I am setting values in the string array(in bean) and setting it to value object.
Later on i want to get the array values from the valueobject and set it to bean by doing this
bean.setAllLRNs(incidentNo
System.out.println("IDEN VALUE" +incidentNotificationVO.ge
BUT incidentNotificationVO.get
individual string elements in it?
thanks
J
ASKER
thanks for the response
I am not sure why its not going through the loop
i gave like this
for(int i=0;i <incidentNotificationVO.ge tAllLRNs() .length;i+ + )
{
System.out.println("IDEN VALUE"+incidentNotificatio nVO.getAll LRNs()[i]) ;
}
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge tAllLRNs() );
its printing "IDEN ArrayObjectVALUE" but not "IDEN VALUE"
I am not sure why its not going through the loop
i gave like this
for(int i=0;i <incidentNotificationVO.ge
{
System.out.println("IDEN VALUE"+incidentNotificatio
}
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge
its printing "IDEN ArrayObjectVALUE" but not "IDEN VALUE"
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
oh yea,,u are damn right..arraylength is 0.
but this is extremely confusing .
when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge tAllLRNs() );
incidentNotificationVO.get AllLRNs() returns a String[] object (some garbage characters)
so how come length of String[] is zero ?
thanks
but this is extremely confusing .
when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge
incidentNotificationVO.get
so how come length of String[] is zero ?
thanks
strange
try casting it and getting the length
((String[]) incidentNotificationVO.get AllLRNs()) .length
fargo
try casting it and getting the length
((String[]) incidentNotificationVO.get
fargo
ASKER
still shows zero
Then you need to check the population of this array. This bean property is not getting set.
fargo
fargo
ASKER
i checked the bean , its getting set since this works perfectly
for(int i=0;i <bean.getAllLRNs().length; i++ )
{
System.out.println("IDEN VALUE"+bean.getAllLRNs()[i ]);
}
setting from bean to value object is also fine since
for(int i=0;i <incidentNotificationVO.ge tAllLRNs() .length;i+ + )
{
System.out.println("IDEN VALUE"+incidentNotificatio nVO.getAll LRNs()[i]) ;
}
works perfectly
BUT when i retrieve from the ValueObject it says array length is zero
for(int i=0;i <bean.getAllLRNs().length;
{
System.out.println("IDEN VALUE"+bean.getAllLRNs()[i
}
setting from bean to value object is also fine since
for(int i=0;i <incidentNotificationVO.ge
{
System.out.println("IDEN VALUE"+incidentNotificatio
}
works perfectly
BUT when i retrieve from the ValueObject it says array length is zero
>>
when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge tAllLRNs() );
incidentNotificationVO.get AllLRNs() returns a String[] object (some garbage characters)
>>
That will print the array reference (empty or not)
when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge
incidentNotificationVO.get
>>
That will print the array reference (empty or not)
ASKER
hi cehj,
sorry, could not understand your question
incidentNotificationVO.get AllLRNs() returns a String[] and prints some garbage on the console . i am assuming its the string[] object.
i want the individual string elements
thanks
sorry, could not understand your question
incidentNotificationVO.get
i want the individual string elements
thanks
If you're using 1.5 you can do
System.out.println(Arrays. toString(i ncidentNot ificationV O.getAllLR Ns()));
but you need to check your setter is being called and that it's setting the property
System.out.println(Arrays.
but you need to check your setter is being called and that it's setting the property
ASKER
no i dont see the toString() for Arrays
Well that's not essential - you can iterate. It's your setter you need to be concerned about
ASKER
ok .what i dont understand is incidentNotificationVO.get AllLRNs() prints some garbage on the console, then
why does System.out.println("array length" +incidentNotificationVO.ge tAllLRNs() ).length ) or
System.out.println ((String[]) incidentNotificationVO.get AllLRNs()) .length) show zero
why does System.out.println("array length" +incidentNotificationVO.ge
System.out.println ((String[]) incidentNotificationVO.get
>>ok .what i dont understand is incidentNotificationVO.get AllLRNs() prints some garbage on the console
As i mentioned above, it will print the array reference irrespective of length
As i mentioned above, it will print the array reference irrespective of length
ASKER
ok so using this reference can i access the string arrayobject or the string elements
thanks
thanks
Not if the length is zero ;-)
ASKER
oh man...u funny
ASKER
problem description in detail
step 1 :: Building VO from formbean
private IncidentsVO buildVOFromFormBean(Action Form actionForm,
HttpServletRequest req, String fromWhere) {
IncidentsFormBean formBn = (IncidentsFormBean) actionForm;
IncidentsVO incidentVO = new IncidentsVO();
String[] lrns = new String[lrnCount];
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs" + i);
}
formBn.setAllLRNs(lrns); //constructed formbean
//setting VO from the constructed form bean
incidentNotificationVO.set AllLRNs(fo rmBn.getAl lLRNs());
}
//next method..
Step 2:: building formbean from VO (just the reverse)
private void buildFormBeanFromIncidents VO(Inciden tsFormBean bn,
IncidentsVO incidentsVO,
HttpServletRequest req) throws
DAOException {
IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTyp eVO();
bn.setAllLRNs(incidentNoti ficationVO .getAllLRN s());
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge tAllLRNs() ); //shows array reference but i want the actual array and elements
}
any help would be greatly appreciated,
thanks
step 1 :: Building VO from formbean
private IncidentsVO buildVOFromFormBean(Action
HttpServletRequest req, String fromWhere) {
IncidentsFormBean formBn = (IncidentsFormBean) actionForm;
IncidentsVO incidentVO = new IncidentsVO();
String[] lrns = new String[lrnCount];
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs"
}
formBn.setAllLRNs(lrns); //constructed formbean
//setting VO from the constructed form bean
incidentNotificationVO.set
}
//next method..
Step 2:: building formbean from VO (just the reverse)
private void buildFormBeanFromIncidents
IncidentsVO incidentsVO,
HttpServletRequest req) throws
DAOException {
IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTyp
bn.setAllLRNs(incidentNoti
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.ge
}
any help would be greatly appreciated,
thanks
Make sure lrnCount > 0
>>//shows array reference but i want the actual array and elements
As has been said before, you iterate it and print it, if there's anything to iterate
>>//shows array reference but i want the actual array and elements
As has been said before, you iterate it and print it, if there's anything to iterate
The following will display the contents of your array (on any version of java):
System.out.println("IDEN ArrayObjectVALUE" +Arrays.asList(incidentNot ificationV O.getAllLR Ns()));
FWhen populating the array check the value of lrnCount
System.out.println("IDEN ArrayObjectVALUE" +Arrays.asList(incidentNot
FWhen populating the array check the value of lrnCount
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Where do you set lrnCount? Or should you be getting it from the request, something like:
lrnCount = Integer.parseInt(request.g etParamete r("lrnCoun t"));
lrnCount = Integer.parseInt(request.g
ASKER
>>>>should you be getting it from the request
yes exactly, its coming from the request. and "lrnCount" works perfect.
yes exactly, its coming from the request. and "lrnCount" works perfect.
What's it's value?
What's its value i mean ;-)
ASKER
i am setting lrnCount in the jsp page and in my action class i am doing
int lrnCount = 0;
if (req.getParameter("lrnCoun t") != null &&
req.getParameter("lrnCount ") != "") {
lrnCount = Integer.parseInt(req.getPa rameter("l rnCount")) ;
System.out.println("Adding "+lrnCount+" elements");
}
so lrncount is either 2 if i enter 2 records or 3 if i enter 3 records.
int lrnCount = 0;
if (req.getParameter("lrnCoun
req.getParameter("lrnCount
lrnCount = Integer.parseInt(req.getPa
System.out.println("Adding
}
so lrncount is either 2 if i enter 2 records or 3 if i enter 3 records.
Let's take it step by step:
int lrnCount = 0;
String sLrnCount = req.getParameter("lrnCount ");
if (sLrnCount != null && (sLrnCount = sLrnCount.trim()).length() > 0) {
lrnCount = Integer.parseInt(sLrnCount );
System.out.println("Adding "+lrnCount+" elements");
}
.........
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs" + i);
System.out.println("lrns[" + i + "]=" + lrns[i]);
}
Can you tell us what the above prints?
int lrnCount = 0;
String sLrnCount = req.getParameter("lrnCount
if (sLrnCount != null && (sLrnCount = sLrnCount.trim()).length()
lrnCount = Integer.parseInt(sLrnCount
System.out.println("Adding
}
.........
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs"
System.out.println("lrns["
}
Can you tell us what the above prints?
ASKER
if aaaaaaaaa is entered in the field value of lrns on browser
System.out.println("lrns["
lrns[0]=aaaaaaaaa
and System.out.println("value of LRNS[i]" +req.getParameter("allLRNs
shows aaaaaaaaa
>>would show
I need to know what it *does* show - not *would* show ;-)
I need to know what it *does* show - not *would* show ;-)
ASKER
sorry i meant "DOES" and not "would"
ASKER
this is fine :
step 1
private IncidentsVO buildVOFromFormBean(Action Form actionForm,
HttpServletRequest req, String fromWhere) {
IncidentsFormBean formBn = (IncidentsFormBean) actionForm;
IncidentsVO incidentVO = new IncidentsVO();
String[] lrns = new String[lrnCount];
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs" + i);
}
formBn.setAllLRNs(lrns); //constructed formbean
//setting VO from the constructed form bean
incidentNotificationVO.set AllLRNs(fo rmBn.getAl lLRNs());
}
THE PROBLEM IS WITH STEP 2
Step 2:: building formbean from VO (just the reverse)
private void buildFormBeanFromIncidents VO(Inciden tsFormBean bn,
IncidentsVO incidentsVO,
HttpServletRequest req) throws
DAOException {
IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTyp eVO();
bn.setAllLRNs(incidentNoti ficationVO .getAllLRN s());
for(int i=0;i <incidentNotificationVO.ge tAllLRNs() .length;i+ + ) //this is not getting executed at all
{
System.out.println("IDEN22 VALUE"+Arrays.asList(incid entNotific ationVO.ge tAllLRNs() ));
}
}
step 1
private IncidentsVO buildVOFromFormBean(Action
HttpServletRequest req, String fromWhere) {
IncidentsFormBean formBn = (IncidentsFormBean) actionForm;
IncidentsVO incidentVO = new IncidentsVO();
String[] lrns = new String[lrnCount];
for (int i = 0; i < lrnCount; i++) {
lrns[i] = req.getParameter("allLRNs"
}
formBn.setAllLRNs(lrns); //constructed formbean
//setting VO from the constructed form bean
incidentNotificationVO.set
}
THE PROBLEM IS WITH STEP 2
Step 2:: building formbean from VO (just the reverse)
private void buildFormBeanFromIncidents
IncidentsVO incidentsVO,
HttpServletRequest req) throws
DAOException {
IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTyp
bn.setAllLRNs(incidentNoti
for(int i=0;i <incidentNotificationVO.ge
{
System.out.println("IDEN22
}
}
>>IncidentsVO incidentVO = new IncidentsVO();
Nothing happens to that as far as i can see
Nothing happens to that as far as i can see
ASKER
do u think its the problem when the data comes back from database to incidentNotificationVO
>>formBn.setAllLRNs(lrns);
Can you post the code for the above?
Can you post the code for the above?
checj that buildFormBeanFromIncidents VO is getting called
ASKER
its working now guys . I realized there was some problem with the data when it was returning back from database.
thanks very much, i greatly appreciate it.
J
thanks very much, i greatly appreciate it.
J
:-)
no worries :)
for(int i=0;i <incidentNotificationVO.ge
System.out.println(inciden
}
fargo