Some help with List and Itterator

In the following method List contains numbers ,

I need to mask those numbers and return them as  **-***1234

for (Iterator it = getList().iterator(); it.hasNext();)
{

if (first)
{
buf.append(" | Number(s): ");
first = false;
}
else
{
buf.append(", ");
}

buf.append(it.next().toString());
}
Who is Participating?

Commented:
this I corrected and tersted:

``````     ArrayList<String> list =new ArrayList<String>();

StringBuffer buf = new StringBuffer("| Numbers: " );
for (Iterator it = list.iterator(); it.hasNext();)
{

String s = (String)it.next();

if(s.length() >4)buf.append("**-***" + s.substring(s.length()-4 ) + ",");
else buf.append(s);

}

System.out.println(buf);
``````
0

Commented:

``````StringBuffer buf = new StringBuffer("| Numbers: " ):
for (Iterator it = getEinList().iterator(); it.hasNext();)
{

int ii = it.next();

String s = "" + ii;

if(s.length() >4)buf.append("**-***" + s.substring(s.length()-4 ) + ",");
else buff.append(s);

}
``````
0

Commented:
So I guess your list is made up of Integer objects - correct?

Or it is made up if String's which are only digits?
0

Author Commented:
I ithink its strings
0

Commented:
If its elements are Integer's, then the above code should work,

if its elementas are String;s, then insted of
---
int ii = it.next();

String s = "" + ii;
---
you  should just use:
---
String s = it.next();
---
0

Commented:
then this should work:

``````StringBuffer buf = new StringBuffer("| Numbers: " ):
for (Iterator it = getEinList().iterator(); it.hasNext();)
{

String s = it.next();

if(s.length() >4)buf.append("**-***" + s.substring(s.length()-4 ) + ",");
else buff.append(s);

}
``````
0

Author Commented:
int ii =it.next();

or string  ii =it.next();

error type mismatch .. cannot convet from object to string/int
0

Commented:
it should be
String s = (String) it.next();
0

Commented:
This is the output of the code which I posted and ran, as above:

``````| Numbers: **-***5678,**-***1234,
``````
0

Commented:
Are you sure your list has String's?

Can you post how this list is declared/created ?
0

Author Commented:
thank you ..it works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.