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?
Code :
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]);
}
}
}
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.
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?
Code :
public String toString()
{
StringBuilder Entry = new StringBuilder();
Entry.append(this.getClass().getName());
return (name+"," + address+","+ telNo.toString());
}
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 :( )
Code :
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:
Code :
System.out.println(myEntries[i]);
Re: array program assistance needed
Hey JavaNoob, not to worry you have all over Christmas before this assignment is due in anyway!
Chris