Results 1 to 5 of 5
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default How to difine type each object is in a ArrayList

    Hello,

    i tried to build a small code to use it in bigger one, anyway i have problem with define the type of the parameters in the ArrayList

    Java Code:
    import java.util.ArrayList;
    
    public class ArrList1 {
    
    protected static ArrayList col1 = new ArrayList();
    	
    	public  void arrList() {
    
    col1.add("hallo");
    col1.add(2);
    col1.add(3);
    col1.add(4.0);
    col1.add(5.5);
    col1.add(6);
    }
     }
    now i want to print the certain value specified by user to print the content of the Arraylist with determining if its type is String. int, float or double

    Java Code:
    import java.util.Scanner;
    
    
    public class test extends ArrList1{
    
    
    	public static void main(String[] args){
    		
    		ArrList1 arr = new ArrList1();
    		arr.arrList();
    		
    		Scanner keyboard = new Scanner (System.in);
    		System.out.print("Please enter Value from 0-5: ");
    		int f = keyboard.nextInt();
    		
    		
    		   if(col1.get(f) != null){
    			 
    	       	 	if (col1.get(f).equals(Integer.TYPE)) 
    	            {
    			       int NewColumn;
    			       NewColumn =  (int) col1.get(f);
    			       System.out.println("Integer" + f );
    	            } 
    	       	 	
    	       	 	else if (col1.get(f).equals(String.class)) 
    	            {
    			       String NewColumn;
    			       NewColumn =  (String) col1.get(f);
    			       System.out.println("String" + f );
    	            }
    	            
    	       	 	else if (col1.get(f).equals(Double.TYPE)) 
    	            {
    			       double NewColumn;
    			       NewColumn =  (double) col1.get(f);
    			       System.out.println("Double" + f );
    	            }
    	            
    	       	 	else if (col1.get(f).equals(Float.TYPE)) 
    	            {
    			       float NewColumn;
    			       NewColumn =  (float) col1.get(f);
    			       System.out.println("Float" + f );
    	            }
    	}
    
    	}
    }
    the problem it print nothing

    why??

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: How to difine type each object is in a ArrayList

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: How to difine type each object is in a ArrayList

    col1.get(f) does not return a Class, it returns the object itself. So code like this:

    Java Code:
    if (col1.get(f).equals(Integer.TYPE))
    is not ever going to resolve to true. Can you now solve it yourself?

    EDIT: PhHein - crosspost detection ninja.
    Last edited by gimbal2; 07-23-2013 at 03:29 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: How to difine type each object is in a ArrayList

    the solution

    Java Code:
    import java.util.Scanner;
    
    
    public class test extends ArrList1{
    
    
    	public static void main(String[] args){
    		
    		ArrList1 arr = new ArrList1();
    		arr.arrList();
    		
    		Scanner keyboard = new Scanner (System.in);
    		System.out.print("Please enter Value from 0-5: ");
    		int f = keyboard.nextInt();
    		
    		Object o = col1.get(f);
    		
    		   if(o != null){
    			 
    	       	 	if (o instanceof Integer) 
    	            {
    			       int NewColumn;
    			       NewColumn =  (int) o;
    			       System.out.println("Integer " + col1.get(f) );
    	            } 
    	       	 	
    	       	 	else if (o instanceof String) 
    	            {
    			       String NewColumn;
    			       NewColumn =  (String) o;
    			       System.out.println("String " + col1.get(f) );
    	            }
    	            
    	       	 	else if (o instanceof Double) 
    	            {
    			       double NewColumn;
    			       NewColumn =  (double) o;
    			       System.out.println("Double " + col1.get(f) );
    	            }
    	            
    	       	 	else if (o instanceof Float) 
    	            {
    			       float NewColumn;
    			       NewColumn =  (float) o;
    			       System.out.println("Float " + col1.get(f) );
    	            }
    
    	}

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,996
    Rep Power
    5

    Default Re: How to difine type each object is in a ArrayList

    that can work, or you could have used o.getClass() to do the equals comparison.

    Annoying code to maintain though, you'll be revisiting this once you want to add a Date, a Boolean or a BigDecimal.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 01-09-2012, 04:46 PM
  2. Accessing an object from within another object from the same type in Java
    By mhasgari in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-20-2011, 04:21 PM
  3. Replies: 0
    Last Post: 04-09-2010, 11:18 AM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 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
  •