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?
 
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;
}
0
 
CEHJCommented:
>>but doesn't seem to work .

In what way?
0
 
CEHJCommented:
Your collection contains String so you can't possibly cast to BigInteger
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
meow00Author Commented:
but it is in the lineArray ... is an "object" now ?!?
0
 
meow00Author Commented:
the iterator part ... I am not sure ...
0
 
CEHJCommented:
It can't be a BigInteger unless it started as one
0
 
hoomanvCommented:
use new BigInteger((String) it.next())
0
 
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 ... ?
0
 
CEHJCommented:
This could work for you

myRecord.setC(new BigInteger((String)it.next());      
0
 
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());      
0
 
hoomanvCommented:
also
Iterator<String> it = lineArray.iterator() ;
0
 
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
0
 
hoomanvCommented:
compile using
javac -source 1.5 yourclass.java
0
 
objectsCommented:
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));
   }


0
 
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

//...
0
 
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'
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.