Iterator of arraylists...

Hello experts,

   I am trying to do the following things, but doesn't seem to work ... can anyone help me fix it ? thanks.

//---------------------------------------------------
       List lineArray = new ArrayList();

        while(st.hasMoreTokens()) {
            String token = st.nextToken();
            lineArray.add(token);
        }

        int i = Integer.parseInt ( (String)lineArray.get(0) ) ;
           
        Iterator it = lineArray.iterator() ;
       
        if (i == 0) {
           
            if(it.hasNext()){
              myRecord.setA( (BigInteger)lineArray. (it.next()));
            } else { return;}

             if(it.hasNext()){
              myRecord.setB( (BigInteger)lineArray. (it.next()));
            } else { return;}

              if(it.hasNext()){
              myRecord.setC( (BigInteger)lineArray. (it.next()));
            } else { return;}


           
        }
LVL 1
meow00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>but doesn't seem to work .

In what way?
CEHJCommented:
Your collection contains String so you can't possibly cast to BigInteger
meow00Author Commented:
but it is in the lineArray ... is an "object" now ?!?
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

meow00Author Commented:
the iterator part ... I am not sure ...
CEHJCommented:
It can't be a BigInteger unless it started as one
hoomanvCommented:
use new BigInteger((String) it.next())
meow00Author Commented:
ooo ... what if it is 3 ... it was just become a string when we tokenize it ... how am I going to change it back to integer ... ?
CEHJCommented:
This could work for you

myRecord.setC(new BigInteger((String)it.next());      
CEHJCommented:
You can eliminate the ugly casting by using generics:

List<String> lineArray = new ArrayList<String>();

int i = Integer.parseInt (lineArray.get(0)) ;

myRecord.setC(new BigInteger(it.next());      
hoomanvCommented:
also
Iterator<String> it = lineArray.iterator() ;
meow00Author Commented:
thanks ... but why I got the following errors when I tried to do:

   List <String>   lineArray = new ArrayList<String>();

----------------------
The type ArrayList is not generic; it can not be parameterized with arguments <String>
Syntax error, parameterized types are only available if source level is 5.0
hoomanvCommented:
compile using
javac -source 1.5 yourclass.java
Mick BarryJava DeveloperCommented:
better off adding the BigInteger's when u create it

     List lineArray = new ArrayList();

        while(st.hasMoreTokens()) {
            String token = st.nextToken();
            lineArray.add(new BigInteger(token));
        }

The rest then becomes:

   BigInteger bi = (BigInteger) lineArray.get(0);
   if (bi.intValue()==0) {
      myRecord.setA(bi);
      if (lineArray.length()>1) myRecord.setB((BigInteger)lineArray(1));
      if (lineArray.length()>2) myRecord.setC((BigInteger)lineArray(2));
   }


d3storCommented:
This one should do the trick.

BigInteger bi;

// Trying to get the first 3 tokens
for(int i = 0; st.hasMoreTokens() && i < 3; i++ ) {
     try {
            bi = new BigInteger(st.nextToken());
    } catch( NumberFormatException e) {
            // Log this or whatever
            return; // Returning anyways, since couldnt parse the string
     }

     // Populating the record object
     if( i = 0 && bi.intValue() == 0)  {
            myRecord.setA(bi);
     } else if( i == 1) {
            myRecord.setB(bi);
     } else if( i == 2) {
            myRecord.setC(bi);
     }
}

if( st.countTokens  <  2 ) return; // Not enough tokens to fill the whole object so returning

//...
Mayank SAssociate Director - Product EngineeringCommented:
BTW, if its an array-list, I won't use an iterator at all but would directly loop through:

for ( int i = 0, count = lineArray .size () ; i < count ; i ++ )
{
  Object o = lineArray.get ( i ) ;
  // do whatever you want with 'o'
}
CEHJCommented:
Rather than having

myRecord.setA
myRecord.setB
myRecord.setC

which are all doing the same thing, you'd be better to make 'myRecord' hold BigInteger[] as an attribute, then you could do something like:

// The following is in your loop
String[] tokens = line.split("\\s*,\\s*");
if (tokens.length > 0) {
      try {
            int n = Integer.parseInt(tokens[0]);
            if (n == 0 && tokens.length >= 2) {
                  for(int i = 1;i < tokens.length;i++) {
                    myRecord.setBigInteger(i - 1, new BigInteger(tokens[i]));
                  }
            }
      }
      catch(NumberFormatException e) {
            continue;
      }
}



// (Method of class of 'myRecord', which holds a BigInteger[] as an instance variable)
.......

public void setBigInteger(int index, BigInteger value) {
      bigInts[index] = value;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.