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());
            }
questjavaAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
this I corrected and tersted:

     ArrayList<String> list =new ArrayList<String>();
    
           list.add("12345678");

           list.add("56781234");

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

Open in new window

0
 
for_yanCommented:


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

0
 
for_yanCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
questjavaAuthor Commented:
I ithink its strings
0
 
for_yanCommented:
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
 
for_yanCommented:
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

0
 
questjavaAuthor Commented:
int ii =it.next();

or string  ii =it.next();

error type mismatch .. cannot convet from object to string/int
0
 
for_yanCommented:
it should be
String s = (String) it.next();
0
 
for_yanCommented:
This is the output of the code which I posted and ran, as above:

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

Open in new window

0
 
for_yanCommented:
Are you sure your list has String's?

Can you post how this list is declared/created ?
0
 
questjavaAuthor 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.

All Courses

From novice to tech pro — start learning today.