Integer doesn't allow use of mod operation?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Integer doesn't allow use of mod operation?

  1. #1
    Join Date
    Feb 2005
    Posts
    22

    Integer doesn't allow use of mod operation?

    My goal is to delete all even numbers in the ArrayList but i'm getting hung up on the mod operation-the error states that: operator % cannot be applied to java.lang.Integer,int:

    import java.util.*;

    class IteratorLab
    {
    public static void main(String [] args)
    {
    //Create ArrayList and add data
    ArrayList numbers = new ArrayList();
    numbers.add("3");
    numbers.add("8");
    numbers.add("4");
    numbers.add("1");
    numbers.add("9");
    numbers.add("7");
    numbers.add("2");
    numbers.add("5");
    numbers.add("6");

    //Print numbersArrayList in original order
    System.out.println( "Original Array List Contents: \n");
    for (Iterator iter = numbers.iterator(); iter.hasNext(){
    System.out.print((String)(iter.next()) + " ");
    }
    System.out.println();

    for (Iterator iter = numbers.iterator(); iter.hasNext()
    {
    Integer value = (Integer)iter.next();
    if(value % 2 != 0) <----------here's where the trouble starts
    iter.remove();
    }
    // Print out the remaining elements with another Iterator.
    System.out.println("\nArrayList after removing all even numbers indexes: \n");
    for (Iterator iter = numbers.iterator (); iter.hasNext (); ) {
    System.out.print (iter.next () + " ");
    }
    System.out.println();
    System.exit(0);
    }

    }

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    1-)Integer class is a wrapper class for int primitive.
    it's objects, are not "numbers" for aritmetic operations.

    Integer i = new Integer(3);
    i.intValue() % 5; // equal 3

    ...
    2-)
    i surprised , you put String objects in ArrayList
    later you cast String objects into Integer objects,
    how this dont give an error ?

  3. #3
    Join Date
    Feb 2005
    Posts
    22

    Causes exception in thread main

    I tried it like this:

    for (Iterator iter = numbers.iterator(); iter.hasNext()
    {
    Integer value = (Integer)iter.next();
    if(value.intValue() % 2 != 0)
    iter.remove();
    }

    and get a ClassCastException

  4. #4
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    yes, you try to cast to Integer from String, its not allowed.

    you can fill ArrayList with Integer objects.
    numbers.add( new Integer(3) );

    insteed of Strings,
    numbers.add("3");

  5. #5
    Join Date
    Feb 2005
    Posts
    22
    Actually - I cast it as a string and then to an Integer and everything works fine now:
    I replaced:
    Integer value = (Integer)iter.next();
    With:
    int value = new Integer((String)(iter.next())).intValue();

    Java doesn't seem to have a problem with this, but would you call it a bad programming practice or is it acceptable?

  6. #6
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    int i = Integer.valueOf("3");// i = 3
    simply this will work too.
    ...
    in java there are primitives and objects.
    int values are primive , Integers are objects .
    everything is not an object, becos this decrease calculation speed.

    ArrayList ( or any other Collection ) can only hold objects.
    so we can not put int values into any Collection,
    Integer objects are used for solving this problem. ( wrapping int values )

    for me , you can do anything else for any particular problem, if it simplfies...

Similar Threads

  1. call function for button
    By angela_quests in forum VB Classic
    Replies: 2
    Last Post: 04-13-2007, 04:57 AM
  2. Physical Address of the NIC
    By eduardoms in forum Security
    Replies: 2
    Last Post: 05-06-2005, 10:26 AM
  3. CopyFileEx API - NullReferenceException
    By Eric Immerman in forum .NET
    Replies: 7
    Last Post: 10-04-2002, 10:12 AM
  4. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center