Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default How do i print my array of data?

    Needs to be sorted in alphabetical surname

    Here's what I have,

    Java Code:
    class Person{
    	private String surName, foreName;
    	private String phNum;
    	Person[] phBook = new Person[3];
    	private int j;
    	void getName(){
    		foreName = Console.readToken();
    		surName = Console.readToken();
    		phNum = Console.readString();		
    	}		
    	public String toString(){
    		 return surName + "," + foreName + "\t" + phNum;
    	} 
    	
    	public boolean order(Person s){																		
    		return  ((surName.compareToIgnoreCase(s.surName))<0 || (surName.equals(s.surName)));		
    	}																
    	
    	void getPersons() {	
    		while(!Console.endOfFile()){
    			for(j = 0; j < 3; j++) {
    				phBook[j] = new Person();
    				phBook[j].getName();		
    				}
    			}	
    		}
    	void printPersons(){				
    		for(int j = 0; j < phBook.length;j++){			
    			System.out.println(phBook[j].toString());			
    		}
    	}
    }
    public class DirectorySort {
    	public static void main(String[] args) {
    		System.out.println("Enter");
    		Person p = new Person();
    		p.getPersons();
    		
    		while(!Console.endOfFile()){
    			Person temp = new Person();
    			temp.getPersons();
    			if(temp.order(p))			
    			p = temp;
    		}
    		p.printPersons();
    	}
    }
    Here's the nearly finished code, I can't seem to get the Boolean compare working though

    Cheers dudes & dudettes :)

    Any info is greatly appreciated..
    Last edited by ManInTheMiddle; 03-28-2012 at 08:40 PM.

  2. #2
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: How do i print my array of data?

    *Please* use the [.CODE] [./CODE] (without the dots) tagsto surround your code. It will format it, and syntax highlight it for you, making it much easier to read and to help you in turn :)
    --Otacon
    Somebody set up us the bomb.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: How do i print my array of data?

    A moderator added code tags to your first post. That won't be done for you every time; learn how to post code correctly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: How do i print my array of data?

    Java Code:
     System.out.println(j);
    will only print the indexes, as you're just printing the counter, that simply increments by one. To get the value in the array, you simply need to print the value of the array at the 'j' position, so

    Java Code:
     System.out.println(phBook[j]);
    instead.
    --Otacon
    Somebody set up us the bomb.

  5. #5
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: How do i print my array of data?

    Cheers man. My apologies..

  6. #6
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: How do i print my array of data?

    For 3 lines of data input


    foreName surName and phNum
    foreName surName and phNum
    foreName surName and phNum


    output

    Person@4f1d0d
    Person@1fc4bec
    Person@dc8569


    This is what I got
    Last edited by ManInTheMiddle; 03-27-2012 at 10:01 PM.

  7. #7
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: How do i print my array of data?

    Right, thats because your phBook is an array of type Person. So when you try to print say phBook[0] it returns you the instance of a Person, but not anything specific about the Person. What exactly are you trying to print? You could always add a toString() method inside of the Person class, which would print any Person information you want, for example
    Java Code:
    public String toString(){
        return "Surname is: "+surName;
    }
    if you added this to the Person class, and then instead of calling
    Java Code:
    System.out.println(phBook[j]);
    in your printPop() method, you could call

    Java Code:
    System.out.println(phBook[j].toString());
    you would get a list not of the Person instances (like Person@dc8569), but of all the surNames instead

    Then depending on what you actually want printed, you can combine them all in the toString() method (or any other method you'd like). Feel free to ask if I went too fast on this one :)
    --Otacon
    Somebody set up us the bomb.

  8. #8
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: How do i print my array of data?

    Cheers man I have it working :)

    Seriously appreciated
    Last edited by ManInTheMiddle; 03-28-2012 at 07:52 PM.

  9. #9
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: How do i print my array of data?

    I have it printing in the format I need but i don't seem to have to sort working

Similar Threads

  1. how to print the String Array?
    By nirasiva in forum New To Java
    Replies: 4
    Last Post: 06-09-2011, 03:07 PM
  2. Print variable data type
    By BillyB in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 04:37 PM
  3. two dimensional array print
    By Myles in forum New To Java
    Replies: 0
    Last Post: 04-26-2010, 11:10 AM
  4. How to print out data from web page to printer????
    By verma1986 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-03-2010, 05:10 AM
  5. Replies: 12
    Last Post: 01-04-2010, 03:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •