Link to home
Start Free TrialLog in
Avatar of questjava
questjavaFlag for United States of America

asked on

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());
            }
Avatar of for_yan
for_yan
Flag of United States of America image



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);

  } 

Open in new window

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

Or it is made up if String's which are only digits?
Avatar of questjava

ASKER

I ithink its strings
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();
---
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);

  } 

Open in new window

int ii =it.next();

or string  ii =it.next();

error type mismatch .. cannot convet from object to string/int
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it should be
String s = (String) it.next();
This is the output of the code which I posted and ran, as above:

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

Open in new window

Are you sure your list has String's?

Can you post how this list is declared/created ?
thank you ..it works