Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Some help with List and Itterator

Posted on 2011-09-21
11
Medium Priority
?
189 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
[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
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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