Solved

Some help with List and Itterator

Posted on 2011-09-21
11
182 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now