Solved

# Some help with List and Itterator

Posted on 2011-09-21
185 Views
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());
}
0
Question by:questjava
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 3

LVL 47

Expert Comment

ID: 36576655

``````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

LVL 47

Expert Comment

ID: 36576663
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 Comment

ID: 36576677
I ithink its strings
0

LVL 47

Expert Comment

ID: 36576679
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

LVL 47

Expert Comment

ID: 36576686
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 Comment

ID: 36576707
int ii =it.next();

or string  ii =it.next();

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

LVL 47

Accepted Solution

for_yan earned 500 total points
ID: 36576719
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

LVL 47

Expert Comment

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

LVL 47

Expert Comment

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

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

LVL 47

Expert Comment

ID: 36576743
Are you sure your list has String's?

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

Author Comment

ID: 36581070
thank you ..it works
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month8 days, left to enroll