Make this code use columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheDD
    New Member
    • Oct 2008
    • 2

    #1

    Make this code use columns

    Hello world, I'm new to the community. I am a beginning programmer of java(well so far) and I got this programming class. I am seeking ideas and help.

    Okay, I got this assignment. I need to make a contact list that has an output like this:
    Code:
    Name        City        Home        Cell        Type    
    Lisa Xiong  Fresno      123-4567    987-5643    Friend  
    Cathy Vang  Fresno      191-6573    987-1546    Contact
    What I want is a column for Name, City, Home, Cell, and Type. And instead of using the \t to tab it to the right place. Here's the code I have which has 2 class:

    Code:
    //first class
    class ContactDemo{
    	public static void main(String args[]){
    		Contact c1 = new Contact();
    		Contact c2 = new Contact();
    		Contact c3 = new Contact();
    		
    		c1.name = "Name";
    		c1.city = "City";
    		c1.home = "Home";
    		c1.cell = "Cell";
    		c1.type = "Type";
    		
    		c2.name = "Lisa Xiong";
    		c2.city = "Fresno";
    		c2.home = "123-4567";
    		c2.cell = "987-5643";
    		c2.type = "Friend";
    		
    		c3.name = "Cathy Vang";
    		c3.city = "Fresno";
    		c3.home = "191-6573";
    		c3.cell = "987-1546";
    		c3.type = "Contact";
    		
    		c1.showName();
    		System.out.print("\t");
    		c1.showCity();
    		System.out.print("\t");
    		c1.showHome();
    		System.out.print("\t");
    		c1.showCell();
    		System.out.print("\t");
    		c1.showType();
    		
    		System.out.println();
    		
    		c2.showName();
    		c2.showCity();
    		System.out.print("\t");
    		c2.showHome();
    		c2.showCell();
    		c2.showType();
    		
    		System.out.println();
    		
    		c3.showName();
    		c3.showCity();
    		System.out.print("\t");
    		c3.showHome();
    		c3.showCell();
    		c3.showType();
    	}
    }
    
    //second class
    class Contact{
    	String name = "";
    	String city = "";
    	String home = "";
    	String cell = "";
    	String type = "";
    	
    	void showName(){
    		System.out.print(name + "\t");
    	}
    	void showCity(){
    		System.out.print(city + "\t");
    	}
    	void showHome(){
    		System.out.print(home + "\t");
    	}
    	void showCell(){
    		System.out.print(cell + "\t");
    	}
    	void showType(){
    		System.out.print(type + "\t");
    	}
    }
    As you can see there is alot of typing(or copy pasting) involve here and I want it to be more simple and easier to put in data. I was think an array would do but not sure how to do that yet. And also I need both of those 2 class, my teacher said something about having a super class and sub class of some sort. Anyway there you go, I hope someone could help me.
    Last edited by TheDD; Oct 16 '08, 06:14 AM. Reason: more detailed title
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    If you don't know about arrays then you simply have to read about them.
    They would certainly make your life easier there. Also compare them with java.util.Array List and pick the one that's most appropriate for this.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Also, you may want to create a function to print a complete contact instead of calling each method manually in each line.

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Simply appending a tab to every column entity doesn't work: if one entry in the
        column exceeds a tab stop length (usually 8 postions) and another entry is
        shorter than this length you're in trouble, eg. "Fresno\t" and "San Fransisco\t"
        don't take up the same amount of space.

        Better look at the PrintWriter.pri ntf() method and its formatted printing capabilities.

        kind regards,

        Jos

        Comment

        • TheDD
          New Member
          • Oct 2008
          • 2

          #5
          Thanks guys for your ideas and help. I did a bit of research and found what I needed.

          Comment

          Working...