Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Saving string value of an iterator Object into a variable.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Saving string value of an iterator Object into a variable.

    Well my problem is that I trying to store the value of it.next in a variable and I have not been successful yet. When I try this:

    strguid = (String) it.next();

    I get this which does not have any specific exception message.
    I am a newbie so my apologies if I am not detailing this problem more.
    This is the full block of code. I think all is coming down from a map type:

    Map attrs = (Map) map.get(SAML2Constants.ATTRIBUTE_MAP);
        if (attrs != null) {
            out.println("<tr>");
            out.println("<td valign=top><b>Attributes: </b></td>");
            Iterator iter = attrs.keySet().iterator();
            out.println("<td>");
    		String strguid = "*";
            while (iter.hasNext()) {
                String attrName = (String) iter.next();
                Set attrVals = (HashSet) attrs.get(attrName);
    			Object[] hashArray = attrVals.toArray();
    			strguid = hashArray.toString();
     
                if ((attrVals != null) && !attrVals.isEmpty()) {
                    Iterator it = attrVals.iterator();
                    while (it.hasNext()) {
                        out.println(attrName + "=" + it.next() + "<br>");
    		    strguid = (Object) it.next().toString(); 
                       //I also tried this strguid = (String) it.next(); and it does not work. 
                    }
                }
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    I get this which does not have any specific exception message.
    Can you explain what you mean? What is returned? Add a println to print out the value of strguid.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    What exactly is happening that you don't expect to happen?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Thank you guys. Well when I try to do this: strguid = (Object) it.next().toString(); or this strguid = (String) it.next(); the code breaks and I get an exception:

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:430)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    root cause

    java.util.NoSuchElementException
    java.util.HashMap$HashIterator.nextEntry(HashMap.j ava:796)
    java.util.HashMap$KeyIterator.next(HashMap.java:82 8)
    org.apache.jsp.fedletSampleApp_jsp._jspService(fed letSampleApp_jsp.java:333)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    I "simply" need to store the value of it.next() into a variable so I can manipulate it. Like adding it to a URL value and passing it to another page.

    out.println(attrName + "=" + it.next() + "<br>");

    This works just fine. The println method is very powerful and it basically converts everything to string.

    But when I try to store the value into a variable my code breaks. I have tried several things and none of it works for me.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    java.util.NoSuchElementException
    Did you see this part of the error message?

    How many times in the loop do you call the next() method? Why do you think there are two elements ready to be returned?
    If you want to do more than one thing with what is returned by next, assign the returned value to a variable and use that variable.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    if ((attrVals != null) && !attrVals.isEmpty()) {
                    Iterator it = attrVals.iterator();
                    while (it.hasNext()) {
                        out.println(attrName + "=" + it.next() + "<br>");
    		    strguid = (Object) it.next().toString(); 
                       //I also tried this strguid = (String) it.next(); and it does not work. 
                    }
                }

    If the loop is good enough to print this: it.next()

    Then why I cannot store the value of it.next() into this:

    strguid = (String) it.next(); or this: strguid = (Object) it.next().toString();

    This is where all my problems lie. I cannot successfully store the value of it.next into a string variable.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    You call hasNext() one time and then call next() two times. Why do you think there is anything to be returned on the second call to next()?

    If you want to do more than one thing with what is returned by next(), assign the returned value to a variable and use that variable.

    Or call hasNext() again to be sure that there is a value to be returned to next()

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Quote Originally Posted by Norm View Post
    You call hasNext() one time and then call next() two times. Why do you think there is anything to be returned on the second call to next()?

    If you want to do more than one thing with what is returned by next(), assign the returned value to a variable and use that variable.

    Or call hasNext() again to be sure that there is a value to be returned to next()

    So...Is it possible to do a it.ToString(); instead of doing a next() on the object?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    .Is it possible to do a it.toString()
    Try it and see what you get.
    It will return some String representation of the iterator class. It won't be anything that you are interested in seeing.

  11. #11
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Oh boy. Yeah I know what you mean. I got all that text which is not what I want to see.

    In my particular case, the println(it.next());

    returns a user name: usernamewxyz

    I want to store that username into a variable. But getting the right string out of it is my main problem I am an absolute newbie to Java, experienced developer but not in Java.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    I want to store that username into a variable
    You should only call next() one time for every call to hasNext();

    If you want to do more than one thing with what is returned by next(), assign the returned value to a variable and use that variable.

    Your code is calling next() twice. Remove one of the calls.

  13. #13
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Thanks.

    Hmm, actually the code is not mine. This is a block of code from Oracle OpenSSO Fedlet --which I installed on one of my servers for an integration with PeopleSoft. Shouldn't there be a way to store it in a variable just the way it is? If println is doing its job and printing the text on the page, should I equally be able to store it in a variable?

    Because the code is part of OpenSSO and not something I wrote from scratch, it's been confusing to me. Does that make sense?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Do you understand that you can only successfully call next() one time for each call to hasNext(). The value that is returned is removed from the iterator. If it was the last value there are no values left for the next time you call the next() method.
    Change the code so that it only calls next() one time for every time call to hasNext() that returns true.

           out.println(attrName + "=" + it.next() + "<br>"); // FIRST TIME HERE
            strguid = (Object) it.next().toString();      // SECOND TIME HERE???

    What if you swapped those statements and used strguid in the println?

  15. #15
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    I will try that :-) And will report back after lunch.

  16. #16
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Quote Originally Posted by Norm View Post
    Do you understand that you can only successfully call next() one time for each call to hasNext(). The value that is returned is removed from the iterator. If it was the last value there are no values left for the next time you call the next() method.
    Change the code so that it only calls next() one time for every time call to hasNext() that returns true.

           out.println(attrName + "=" + it.next() + "<br>"); // FIRST TIME HERE
            strguid = (Object) it.next().toString();      // SECOND TIME HERE???

    What if you swapped those statements and used strguid in the println?
    while (it.hasNext()) {
      strguid = (String) it.next();
      out.println(attrName + "=" + strguid + "");

    You were right. That fixed the problem man!

  17. #17
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving string value of an iterator Object into a variable.

    Thank you everybody! Special thanks to Norm!

Similar Threads

  1. returning variable and saving to file
    By ravencrest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 09:04 AM
  2. Saving session info in a context object
    By bhask1 in forum Web Frameworks
    Replies: 0
    Last Post: September 19th, 2011, 02:14 PM
  3. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  4. saving list of data from JSP into java object
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: January 12th, 2011, 11:24 AM
  5. Convert object to String
    By innspiron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:48 AM

Tags for this Thread