Jump to content

Java Code for taking 10 instances and print them out

- - - - -

  • Please log in to reply
2 replies to this topic

#1
jambor

jambor

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

I am new to java and have created my first arraylist.
1) The iterator is not functioning!

2)The problem is that it is repeating the same names in main(). I tried a nested for loop but it did not work!!
I tried something like this but it did not work. Can anyone help?

package person;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.Random;


/**

*

* @author borgj062

*/

public class Person

{

String name, surname;

String address;

int day;

int month;

int year;

int age;

static ArrayList<Person> displayList = new ArrayList<Person>();




public void setName(String n )

{

name = n;

}

public void setSurname(String s)

{

surname = s;

}

public void setAddress(String add)

{

address = add;

}

public void setDate(int d)

{

day = d;

}

public void setMonth(int m)

{

month = m;

}

public void setYear(int y)

{

year = y;

}

public String getName()

{

return name;

}

public String getSurname()

{

return surname;

}

public String getAddress()

{

return address;

}

public int getDay()

{

return day;

}

public int getMonth()

{

return month;

}

public int getYear()

{

return year;

}


public static void randomizer()

{



Person person0 = new Person();

Person person1 = new Person();

Person person2 = new Person();

Person person3 = new Person();

Person person4 = new Person();

Person person5 = new Person();

Person person6 = new Person();

Person person7 = new Person();

Person person8 = new Person();

Person person9 = new Person();


displayList.add(person0);

displayList.add(person1);

displayList.add(person2);

displayList.add(person3);

displayList.add(person4);

displayList.add(person5);

displayList.add(person6);

displayList.add(person7);

displayList.add(person8);

displayList.add(person9);




person0.setName("David");

person1.setName("James");

person2.setName("Clayton");

person3.setName("Jason");

person4.setName("Natasha");

person5.setName("Alfred");

person6.setName("Shaun");

person7.setName("Ingrid");

person8.setName("Mariella");

person9.setName("Marvic");


person0.setSurname("Notabile");

person1.setSurname("Borg");

person2.setSurname("Breil");

person3.setSurname("Kits");

person4.setSurname("Micallef");

person5.setSurname("Bonnici");

person6.setSurname("Delia");

person7.setSurname("Buhagiar");

person8.setSurname("Douglas");

person9.setSurname("Mallia");


person0.setDate(12);

person1.setDate(7);

person2.setDate(13);

person3.setDate(10);

person4.setDate(22);

person5.setDate(30);

person6.setDate(23);

person7.setDate(30);

person8.setDate(11);

person9.setDate(19);


person0.setMonth(7);

person1.setMonth(8);

person2.setMonth(12);

person3.setMonth(9);

person4.setMonth(11);

person5.setMonth(2);

person6.setMonth(8);

person7.setMonth(10);

person8.setMonth(1);

person9.setMonth(5);


person0.setYear(1990);

person1.setYear(1991);

person2.setYear(1993);

person3.setYear(1994);

person4.setYear(1995);

person5.setYear(1996);

person6.setYear(1997);

person7.setYear(1998);

person8.setYear(1999);

person9.setYear(2000);


person0.setAddress("21, Triq il-Kalvarju Mosta");

person1.setAddress("212,Triq il-Berebies Zabbar");

person2.setAddress("99, Triq il-Kuncizzjoni Bormla");

person3.setAddress("19, Triq it-Tempji Neolitici Tarxien");

person4.setAddress("112, Triq il-Fawwara Fgura");

person5.setAddress("78, Manwel Dimech Street, Saflieni");

person6.setAddress("111, Triq in-Nahal, Mellieha");

person7.setAddress("113, Triq il-Qaliet, Fawwara");

person8.setAddress("122, Triq il-Lampuka, Rahal Gdid");

person9.setAddress("8, Triq in-Nassa, San Pawl il-Bahar");


displayList.get(0);

displayList.get(1);

displayList.get(2);

displayList.get(3);

displayList.get(4);

displayList.get(5);

displayList.get(6);

displayList.get(7);

displayList.get(8);

displayList.get(9);


// displayList.remove(person9);


}

// I had a problem with the iterator - in main it is giving me a concatenation of strange numbers???

//public static void usingIterator()

//{

// Iterator it = displayList.iterator();

// while(it.hasNext())

// {

// System.out.println(it.next());

//

// }

// System.out.println(displayList);

//}



public static void generateRandomPerson()

{


Random myNumber = new Random();

int t = myNumber.nextInt(9);




System.out.printf("%-10s%-10s%30s\n",displayList.get(t).name);//displayList.get(t).surname,displayList.get(t).addr ess);

//System.out.printf("Date of birth %02d/%02d/%04d\n\n\n", displayList.get(t).day, displayList.get(t).month,displayList.get(t).year);


}

}


Here is the main 


public static void main(String[] args) 

{

Person p = new Person();



for(int counter =0; counter < 10; counter++)

{



Person.randomizer();

Person.generateRandomPerson();

// Person.usingIterator();

}


}

}


Tags: None

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The strange numbers the iterator shows are propably the address numbers it uses in the memory.
Java does not know how you want it to display a Person.

To tell Java how to, override the tostring method:

@Override

public String toString(){

  return name + " " + surname + ": " + address;

}

I think this will solve many problems.

#3
liyuyu_852000

liyuyu_852000

    Newbie

  • Members
  • Pip
  • 2 posts
Change that line like this,the output is right,I tested

System.out.printf("%s\n",displayList.get(t).name);




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users