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:
Code Java:
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.
}
}
}
Re: Saving string value of an iterator Object into a variable.
Quote:
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.
Re: Saving string value of an iterator Object into a variable.
What exactly is happening that you don't expect to happen?
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)
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.
Re: Saving string value of an iterator Object into a variable.
Quote:
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.
Re: Saving string value of an iterator Object into a variable.
Code java:
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.
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()
Re: Saving string value of an iterator Object into a variable.
Quote:
Originally Posted by
Norm
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?
Re: Saving string value of an iterator Object into a variable.
Quote:
.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.
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.
Re: Saving string value of an iterator Object into a variable.
Quote:
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.
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?
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.
Code :
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?
Re: Saving string value of an iterator Object into a variable.
I will try that :-) And will report back after lunch.
Re: Saving string value of an iterator Object into a variable.
Quote:
Originally Posted by
Norm
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.
Code :
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?
Code java:
while (it.hasNext()) {
strguid = (String) it.next();
out.println(attrName + "=" + strguid + "");
You were right. That fixed the problem man!
Re: Saving string value of an iterator Object into a variable.
Thank you everybody! Special thanks to Norm!