Daniel Perera wrote:
I'm able to randomly generate the initials and surnames of the entries but unfortunately the extensions aren't working. The file is being created with initials and surnames but no extensions are printed
Junilu Lacar wrote:Hi Daniel,
Welcome to the Ranch!
Daniel Perera wrote:
I'm able to randomly generate the initials and surnames of the entries but unfortunately the extensions aren't working. The file is being created with initials and surnames but no extensions are printed
Please read this: ItDoesntWorkIsUseless
What were you expecting exactly and what actually happened? Be very specific; the more details, the better.
Tim Driven Development | Test until the fear goes away
Tim Cooke wrote:A couple of things that I can see.
1) At line 31 You have not defined what type of output the printf is to expect. You say "%04" as precision but of what type? perhaps "%04d" for decimal?
2) Also at line 31 you are attempting to print the whole ArrayList extensions. I guess you wanted to just print a single item from the List?
OCAJP 7
Junilu Lacar wrote:You don't need the for-loop (line 15) around extensions. You just need to move a couple of lines in that for-loop so that you're printing out a random extension on every iteration that generates a line in your output file.
Campbell Ritchie wrote:Welcome to the Ranch
Why have you got so many static members in that class? Why have you not got a PhoneNumber class?
Stuie Clarky wrote:Have you considered creating an object that stores a contract directory ( so an initial, a name, and an extension) . You could then create an array of those objects during the generation part, then printing out is simply iterating through the object array and printing each one to the file.
OCAJP 7
Stuie Clarky wrote:With the regards to the static variable stuff, this is because you are mostly working in the Main() method. This is generally considered bad practice for a number of reasons. I think you should take a step back from the actual coding for now, and go through the spec with a pencil and sketch things out and really consider what you need to do. Java is built around the concept of objects, at a high level these tend to be classes, so like a phone extension could be an object, a name could be an object. Then each object will have properties associated with it etc. Once you have a general, high level idea of what you need to do, then go back to code.
Your Main() class will then create instances of the objects you need, and you can manipulate them without being restricted by the static context.
Daniel Perera wrote:
Do I not need the for loop so that a random extension is only added to the arraylist if it is not already in the arraylist, i.e. to ensure there are no duplicates?
... but I'd also like to know why the current code isn't doing what I want it to do...
In which case all uses of the keyword static except before void main are to be regarded as a mistakeDaniel Perera wrote: . . . To be perfectly honest, I don't really understand the concept of static/non static members too well. . . .
That sounds like my phone number class. Only explained properly. Yes, it is necessary. I suggest you need a phone demo class, which has a one‑line main method and nothing else in:-You will obviously something to set up the array, to fill it with contact objects, to print some out, etc., etc., which you can call from the demonstrateNumbers() method.
With regards to the PhoneNumber class I didn't think it was necessary. As I said this class is just to create a random directory for use in my project, as it would take ages to generate such a file manually.
. . .Stuie Clarky wrote:Have you considered creating an object that stores a contract directory ( so an initial, a name, and an extension) . You could then create an array of those objects during the generation part, then printing out is simply iterating through the object array and printing each one to the file.
Only I think by contract he meant contactHave you considered creating an object that stores a contract directory ( so an initial, a name, and an extension) .
Daniel Perera wrote:I don't want to spend too much time on this class, and I think that as long as it can generate the file with the initials, surnames and extensions as I need, that would be fine. So I would prefer to work with what I have..
Daniel Perera wrote: generate a phone directory of 10,000 entries ... 4-digit extensions that are unique
OCAJP 7
Tim Driven Development | Test until the fear goes away
Junilu Lacar wrote:
Daniel Perera wrote:I don't want to spend too much time on this class, and I think that as long as it can generate the file with the initials, surnames and extensions as I need, that would be fine. So I would prefer to work with what I have..
The best way to go fast is to go well.
Clean, understandable, well-structured code is easier to read and debug. I give the same amount of care and attention to utility code like this as I give to production code.
You should at least break it down into smaller chunks such that different methods take care of different responsibilities. So far, these are the tasks I can identify: generate a random surname, generate a random initial, generate a unique extension (random). That's at least 4 methods, including main(). Right now you only have 1 method, main().
Daniel Perera wrote:Right now I populate the extensions arraylist in the main method with a for loop that goes through all values from 0 to 9999. I may have the wrong idea, but I was thinking that the randomExtension method should use collections.shuffle and then return an extension by doing extensions.get. Am I right in doing it this way?
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|