Solved

Some help with List and Itterator

Posted on 2011-09-21
11
183 Views
Last Modified: 2012-05-12
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
Comment
Question by:questjava
  • 8
  • 3
11 Comments
 
LVL 47

Expert Comment

by:for_yan
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);

  } 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
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

by:questjava
ID: 36576677
I ithink its strings
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 47

Expert Comment

by:for_yan
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

by:for_yan
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);

  } 

Open in new window

0
 

Author Comment

by:questjava
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

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

Expert Comment

by:for_yan
ID: 36576722
it should be
String s = (String) it.next();
0
 
LVL 47

Expert Comment

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

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

Open in new window

0
 
LVL 47

Expert Comment

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

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

Author Comment

by:questjava
ID: 36581070
thank you ..it works
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 58
varialbe initialization 11 32
servlet example 17 29
mysql jsp example issue 32 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question