?
Solved

reading elements from string array

Posted on 2006-03-24
40
Medium Priority
?
294 Views
Last Modified: 2010-05-18
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(incidentNotificationVALUEOBJ.getAllLRNs());
System.out.println("IDEN VALUE" +incidentNotificationVO.getAllLRNs());

BUT incidentNotificationVO.getAllLRNs()  prints/gives me the Array object. HOW do i get to see the
individual string elements in it?

thanks
J
0
Comment
Question by:jaggernat
  • 17
  • 14
  • 5
  • +1
40 Comments
 
LVL 11

Expert Comment

by:fargo
ID: 16284943
you have to iterate over the string array.

for(int i=0;i <incidentNotificationVO.getAllLRNs().length;i++  ){
System.out.println(incidentNotificationVO.getAllLRNs()[i]);
}

fargo
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285006
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.getAllLRNs().length;i++  )
        {
           System.out.println("IDEN VALUE"+incidentNotificationVO.getAllLRNs()[i]);
         }

        System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.getAllLRNs());

its printing "IDEN ArrayObjectVALUE"  but not   "IDEN VALUE"
0
 
LVL 11

Assisted Solution

by:fargo
fargo earned 400 total points
ID: 16285025
Hi,

It means the incidentNotificationVO.getAllLRNs().length is 0.
you have no elements in the array.

print out
System.out.println(incidentNotificationVO.getAllLRNs().length);

fargo
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:jaggernat
ID: 16285073
oh yea,,u are damn right..arraylength is 0.

but this is extremely confusing .

when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.getAllLRNs());

incidentNotificationVO.getAllLRNs() returns a String[] object  (some garbage characters)

so how come length of String[] is zero ?

thanks
0
 
LVL 11

Expert Comment

by:fargo
ID: 16285091
strange

try casting it and getting the length

((String[]) incidentNotificationVO.getAllLRNs()).length

fargo
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285127
still shows zero
0
 
LVL 11

Expert Comment

by:fargo
ID: 16285159
Then you need to check the population of this array. This bean property is not getting set.

fargo

0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285293
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.getAllLRNs().length;i++  )
        {
           System.out.println("IDEN VALUE"+incidentNotificationVO.getAllLRNs()[i]);
         }

works perfectly

BUT when i retrieve from the ValueObject it says array length is zero


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285425
>>
when i give
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.getAllLRNs());

incidentNotificationVO.getAllLRNs() returns a String[] object  (some garbage characters)
>>

That will print the array reference (empty or not)
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285458
hi cehj,

sorry, could not understand your question

 incidentNotificationVO.getAllLRNs() returns a String[] and prints some garbage on the console . i am assuming its the string[] object.
i want the individual string elements

thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285468
If you're using 1.5 you can do


System.out.println(Arrays.toString(incidentNotificationVO.getAllLRNs()));

but you need to check your setter is being called and that it's setting the property
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285485
no i dont see the toString() for Arrays

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285496
Well that's not essential - you can iterate. It's your setter you need to be concerned about
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285514
ok .what  i dont understand is        incidentNotificationVO.getAllLRNs()   prints some garbage on the console, then

why does System.out.println("array length" +incidentNotificationVO.getAllLRNs()).length ) or
System.out.println ((String[]) incidentNotificationVO.getAllLRNs()).length)  show  zero
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285530
>>ok .what  i dont understand is        incidentNotificationVO.getAllLRNs()   prints some garbage on the console

As i mentioned above, it will print the array reference irrespective of length
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285547
ok   so using this reference can i access the string arrayobject  or the string elements

thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285564
Not if the length is zero ;-)
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285567
oh man...u funny
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16285633
problem description in detail

step 1 :: Building VO from formbean

 private IncidentsVO buildVOFromFormBean(ActionForm 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.setAllLRNs(formBn.getAllLRNs());  

}

//next method..

Step 2:: building formbean from VO (just the reverse)

private void buildFormBeanFromIncidentsVO(IncidentsFormBean bn,
                                              IncidentsVO incidentsVO,
                                              HttpServletRequest req) throws
            DAOException {
             IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTypeVO();
 bn.setAllLRNs(incidentNotificationVO.getAllLRNs());
System.out.println("IDEN ArrayObjectVALUE" +incidentNotificationVO.getAllLRNs());   //shows array reference but i want the actual array and elements
}


any help would be greatly appreciated,
thanks

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16285660
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
0
 
LVL 92

Expert Comment

by:objects
ID: 16285885
The following will display the contents of your array (on any version of java):

System.out.println("IDEN ArrayObjectVALUE" +Arrays.asList(incidentNotificationVO.getAllLRNs()));

FWhen populating the array check the value of lrnCount
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 total points
ID: 16285888
>>FWhen populating the array check the value of lrnCount

Already mentioned
0
 
LVL 92

Assisted Solution

by:objects
objects earned 600 total points
ID: 16286194
try adding the following:

private IncidentsVO buildVOFromFormBean(ActionForm actionForm,
                                            HttpServletRequest req, String fromWhere) {

        IncidentsFormBean formBn = (IncidentsFormBean) actionForm;
        IncidentsVO incidentVO = new IncidentsVO();
        System.out.println("Adding "+lrnCount+" elements");
        String[] lrns = new String[lrnCount];
        for (int i = 0; i < lrnCount; i++) {
                lrns[i] = req.getParameter("allLRNs" + i);
                System.out.println(i+":"+lrns[i]);
        }
        ......

That will show you in the log whats getting read.
0
 
LVL 92

Expert Comment

by:objects
ID: 16286199
Where do you set lrnCount?  Or should you be getting it from the request, something like:

lrnCount = Integer.parseInt(request.getParameter("lrnCount"));
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16299783
>>>>should you be getting it from the request

yes exactly, its coming from the request. and "lrnCount" works  perfect.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16299797
What's it's value?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16299805
What's its value i mean ;-)
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16299887
i am setting lrnCount in the jsp page and in my action class i am doing
int lrnCount = 0;
            if (req.getParameter("lrnCount") != null &&
                req.getParameter("lrnCount") != "") {
                lrnCount = Integer.parseInt(req.getParameter("lrnCount"));
                System.out.println("Adding "+lrnCount+" elements");

           
            }

so lrncount is either 2 if i enter 2 records or 3 if i enter 3 records.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16300044
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?
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16300613



 if aaaaaaaaa is entered in the field value of lrns on browser

System.out.println("lrns[" + i + "]=" + lrns[i]);  would show
lrns[0]=aaaaaaaaa

and System.out.println("value of LRNS[i]" +req.getParameter("allLRNs" + i));
shows aaaaaaaaa
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16300711
>>would show

I need to know what it *does* show - not *would* show ;-)
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16300747
sorry i meant "DOES" and not "would"
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16300779
this is fine :
step 1
private IncidentsVO buildVOFromFormBean(ActionForm 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.setAllLRNs(formBn.getAllLRNs());  

}


THE PROBLEM IS WITH STEP 2
Step 2:: building formbean from VO (just the reverse)

private void buildFormBeanFromIncidentsVO(IncidentsFormBean bn,
                                              IncidentsVO incidentsVO,
                                              HttpServletRequest req) throws
            DAOException {
             IncidentTypeVO incidentTypeVO = incidentsVO.getIncidentTypeVO();
 bn.setAllLRNs(incidentNotificationVO.getAllLRNs());

for(int i=0;i <incidentNotificationVO.getAllLRNs().length;i++  ) //this is not getting executed at all
        {
                    System.out.println("IDEN22 VALUE"+Arrays.asList(incidentNotificationVO.getAllLRNs()));
       
                 
       }



}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16300926
>>IncidentsVO incidentVO = new IncidentsVO();

Nothing happens to that as far as i can see
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16301040
do u think its the problem when the data comes back from database to incidentNotificationVO
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16302260
>>formBn.setAllLRNs(lrns);

Can you post the code for the above?
0
 
LVL 92

Expert Comment

by:objects
ID: 16304278
checj that buildFormBeanFromIncidentsVO is getting called
0
 
LVL 10

Author Comment

by:jaggernat
ID: 16310345
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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16313209
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16314955
no worries :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question