Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to difine type each object is in a ArrayList

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

    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

    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
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

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

    Crossposted: How to difine type each object is in a ArrayList

  3. The Following User Says Thank You to PhHein For This Useful Post:

    jps (July 25th, 2013)

Similar Threads

  1. Creating Method that returns a casted object type
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2012, 01:23 PM
  2. how to compare 2 different type object list
    By enginco in forum Collections and Generics
    Replies: 3
    Last Post: April 3rd, 2012, 03:45 PM
  3. [SOLVED] what data type should i use in MySql for a java object to be saved
    By chronoz13 in forum JDBC & Databases
    Replies: 4
    Last Post: October 9th, 2011, 07:17 AM
  4. How to perform operations on arguments with type OBJECT?
    By hexwind in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 3rd, 2011, 12:17 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM