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 7 of 7

Thread: need help in creating array with multiple data type in java

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help in creating array with multiple data type in java

    i am complete beginner, i use eclipse.
    i need to create an array with attributes name, gender, phone, age.
    and then sort acording to age in ascending order.

    i created like this,
     public class Array{  
        	private String[] name={"ram", "katy", "priti", "john"};
        	private String[] gender={"male","female","female","male"};
        	private int[] phone={989898089,89898989,8982989089,898908989};
        	private int[] age={45,24,30,28};
     
     
        	public void printarray(){
        		for(int i=0;i<=4;i++)
        			System.out.printf("%s %s %d %d \n",name[i],gender[i],phone[i],age[i]);
     
     
        		// sorting 
        	for(int i=0; i<4;++i)
            {
     
        	for(int j=i+1; j<5; ++j)
        			{
        				if(age[i]>age[j])
                           {
        	                int a =  age[i];
     
        	                age[i] = age[j];
     
        	                age[j] = a;	
        		        }
     
        			}
     
     
        		}
        	    for (int i = 0; i < 5; ++i){
     
        	    	System.out.printf("%s %s %s %d \n",name[i],gender[i],mail[i],marks[i]);
        	    }
     
     
     
        	}
        }
    This code sorts the age attribute alone and when printing ouput it swaps one person's age to other person, how to make it correct
    Last edited by jakeaustin; October 10th, 2014 at 05:23 AM.


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: need help in creating array with multiple data type in java

    Well, you want to sort the 4 attributes, so when you loop through the ages, why do you only move the age? Why not move the values in the other 3 arrays as well?

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help in creating array with multiple data type in java

    it worked, thanks..

    is it possible to make custom array lik this book b1=new book(101,"katy","age"); where 101 should be stored in int roll no;
    katy in string name and age in int age

    --- Update ---

    it worked, thanks..

    is it possible to make custom array lik this book b1=new book(101,"katy","age"); where 101 should be stored in int roll no;
    katy in string name and age in int age
    and how to sort age if i give like this

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: need help in creating array with multiple data type in java

    Yes, but you have a bit to learn first, below is some example code for you to peruse. See how much you can understand. Have a look on google for ' java objects' and read around. If you have questions, post, but I will probably redirect you to a website with an explanation

    public class Untitled 
    {
    	public static void main(String[] args) 
    	{
    		Person[] listOfPeople = new Person[2];
    		listOfPeople[0] = new Person("Bill", "Male");
    		listOfPeople[1] = new Person("Andy", "Male");
     
    		System.out.println(listOfPeople[0]);
                    System.out.println(listOfPeople[0].name);
    	}
    }
     
    class Person
    {
    	public String name, gender;
     
    	Person(String name, String gender)
    	{
    		this.name = name;
    		this.gender = gender;
    	}
     
    	public String toString()
    	{
    		return "Name: "+name+", gender: "+gender;
    	}
    }

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help in creating array with multiple data type in java

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help in creating array with multiple data type in java

    ok, done.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need help in creating array with multiple data type in java

    is it possible to make custom array lik this book b1=new book(101,"katy","age");
    This is not suggesting a "custom array" but a class of your design called "Book", not "book". In Java, class names begin with capital letters (for a good reason, mostly convention). Then,

    Book b1 = new Book(101,"katy","age");

    calls a Book() constructor with the parameters 101, "katy", and "age" that can be used as you described. You might want to review the tutorial Providing Java Constructors. . . .

Similar Threads

  1. [SOLVED] how to convert a input array of type Strings to a class type
    By adit in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 17th, 2014, 07:46 PM
  2. Replies: 1
    Last Post: May 8th, 2014, 06:40 AM
  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. array to store multiple names and data
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 05:03 AM
  5. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM

Tags for this Thread