Solved

Some help with List and Itterator

Posted on 2011-09-21
11
181 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
Comment Utility


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
Comment Utility
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
Comment Utility
I ithink its strings
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:questjava
Comment Utility
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
Comment Utility
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
Comment Utility
it should be
String s = (String) it.next();
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
Are you sure your list has String's?

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

Author Comment

by:questjava
Comment Utility
thank you ..it works
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

743 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

8 Experts available now in Live!

Get 1:1 Help Now