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

Thread: array program assistance needed

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation array program assistance needed

    Hey guys i was wondering whether anyone could help me out ive got to create a program that creates three directory entries and place them in an array of length 10. Once the entries have been placed in the array a for loop should be used to retrieve each entry from the array and display it on the screen. im stuck lol this is what i have so far. im unsure as to how i can print off my array as a string?

    import java.util.*;
    public class ArrayEntries
    {
     
     
     
    	public static void main(String[] args)
    	{
     
     
    		Entry []  = new Entry[10];
    		// create three entries and store in array
    		Entry [0] = ("Garry Julesl");
    		Entry [1] = ("143 Treseifion");
    		Entry [2] = ("01407760799");
    		Entry [3] = ("Liza Williams");
    		Entry [4] = ("2 Gil Street");
    		Entry [5] = ("01407723233");
    		Entry [6] = ("Louise Jones");
    		Entry [7] = ("2 Llainoch");
    		Entry [8] = ("01407760000");
     
    		// print only the three entries
    		for(int i= 0; i< Entry.length; i++)
    		{
    			System.out.println(Entry[i]);
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: array program assistance needed

    All the code you have here is correct. However, I'm suspecting that you don't have the toString() method in the Entry class defined right.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: array program assistance needed

    hey helloworld922 many thanks for the reply. appreciate it.. i have defined it here is my sample. i have another program working off my entry class also. could it be possible to run 2 programs off the one class?

    public String toString()
    	{
    		StringBuilder Entry = new StringBuilder();
    		Entry.append(this.getClass().getName());
     
    		return (name+"," + address+","+ telNo.toString());
     
     
    	}

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: array program assistance needed

    I'm not sure what you mean by running two programs off of one class... Do you mean having two different classes using the Entry class? In that case, yes. That's the beauty of object-oriented programming. It allows great code re-use so in theory you only need to write code once and then it can be used anywhere you want it to.

    Ahh, I found the problem in your main method! You forgot to actually create the new objects before stuffing them in the array. Also, you didn't create a variable to hold the information (kind of dumb of me to miss this )

    Entry[] myEntries = new Entry[10];
    myEntries[0] = new Entry("Gary Jules!");
    myEntries[1] = new Entry("143 Treseifion");
    // etc. etc.

    The printing out part should then reference the myEntries variable rather than the Entry class:

    System.out.println(myEntries[i]);

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: array program assistance needed

    Hey JavaNoob, not to worry you have all over Christmas before this assignment is due in anyway!

    Chris

Similar Threads

  1. Replies: 5
    Last Post: December 13th, 2009, 07:10 PM
  2. Array program help needed
    By SCM in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2009, 11:28 PM
  3. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM
  4. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM
  5. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 02:19 AM