aspose file tools*
The moose likes Beginning Java and the fly likes Creating random names and phone extensions, help please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Creating random names and phone extensions, help please!

Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Hi,

I am a beginner in Java and I'm currently working on a Phone Directory project for university. Since the project is supposed to be for a large phone directory I've created a class that is supposed to generate a phone directory of 10,000 entries (initials, surnames and 4-digit extensions that are unique) which I can then work with. 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. Can anyone tell me what I'm doing wrong? Also, any general feedback and ways to make this better would be great.

Here is the code:



Thanks for your help in advance!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
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.


Hi. I'm sorry if I wasn't clear, I thought I made it pretty clear... I'm expecting the program to write a file called RandomDirectory with random combinations of initials, surnames and 4-digit extensions. So the file should be a txt file that has entries like this:

D Perera 0238
J James 2495

What is currently happening is that the file only has initials and surnames, but no extension numbers, like this:

D Perera
J James

Is that more clear?
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1014
    
  47

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?

You would have seen clues towards these problems as Runtime Exceptions so it's well worth checking these when you're experiencing unexpected behaviour in your programs.


Tim Driven Development
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
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?



Thanks for the reply. Yeah line 31 was supposed to say %04d, I actually removed that before as it gave an IllegalFormatConversionException (which was going to be my next question) and tested to see what happened without the %04d, and then put it back in when I was posting the question here, so that's just a typo.

What I wanted to do was for the 10,000 entries, print an item from the list to go along with each entry, so one extension to follow the randomly generetad initial and surname. As I stated above, the output should be a text file like this:

D Perera 7250
K Joe 2585
L Morrison 0012

And so on. If printing extensions is wrong, how would I accomplish this?

Thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
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
Ranch Hand

Joined: Nov 09, 2012
Posts: 73

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
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
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.


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?

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?


To be perfectly honest, I don't really understand the concept of static/non static members too well. Initially, I did not make those members static, but Java gave me the "cannot make a static reference to a non-static member" error and I basically took the advice it gave me and made those members static. Perhaps not the best idea but I'm in a bit of a hurry so didn't really want to spend too much time on that. 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.


I hadn't considered that, no. It actually sounds like a good idea, I may give it a try but I'd also like to know why the current code isn't doing what I want it to do...

Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 73

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.

In regards to your current issue, look at how you are using your 'extensions' array in comparison to the other arrays. How do you know which to print?
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
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.


Thanks for the reply. The thing is that this class is actually not a part of the spec - I mean, the project is to write a program which allows a user to add new entries, delete entries, change extensions, and lookup entries etc in a phone directory. We need to make the program efficient and so I am trying to generate a large phone directory (approx 10,000 to 25,000 entries) so that I can do my testing with that directory. As it is not a part of the spec, 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..
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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?

You got me there but that's not how I would do it. First, you really should consider making a proper class and not putting everything in main(). Then, you need to delegate responsibilities, like say an ExtensionGenerator class that provides extension numbers and manages the randomness and uniqueness of those numbers internally.

... but I'd also like to know why the current code isn't doing what I want it to do...

You have already been given enough hints to be able to figure that one out. Printing extensions is not the way to go because extensions is a collection of numbers, not just a single number. Look up the documentation of ArrayList and see what methods you should use to get (hint hint) a specific element. If you do it the way I suggest above, all that business with the ArrayList gets hidden in the ExtensionGenerator class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
Daniel Perera wrote: . . . To be perfectly honest, I don't really understand the concept of static/non static members too well. . . .
In which case all uses of the keyword static except before void main are to be regarded as a mistake

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.
. . .
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.
Stuie Clarky has told you already exactly what goes in the contact class
Have you considered creating an object that stores a contract directory ( so an initial, a name, and an extension) .
Only I think by contract he meant contact
I shall have to go back to your original post and edit it because the lines were too long (see this FAQ).
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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().
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Daniel Perera wrote: generate a phone directory of 10,000 entries ... 4-digit extensions that are unique

If you need 10000 entries each with a unique 4 digit extension then you are going to have to use every value between 0 and 9999. Trying to generate these randomly is a bad idea - it's possible that one or more of the values might never appear (or at least take a very long time).
You could just assign the values in order from 0 to 9999 or if you do want them to be a little more randomly assigned, put all the values in an ArrayList and then use Collections.shuffle to mix them up.

I would also say that using different data every time you test could be problematic - if you hit a bug that is related to specific values of the data, it may be hard to reproduce if you use a different set of data the next time you run the test. Far better to just generate the data once and store it in a file and then read the data from the file every time.
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 73

@Cambell, yes I meant contact, I am writing code using contracts at work currently so a bit blind to the typo :P Cheers
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Also, if you're going to use it for testing, then I wouldn't even write it out to a file. I have a feeling that if you do it this way, it will actually lead you to the realization that PhoneBook is a production class and the fact that the phone book entries are in a file is just an implementation detail.

(Edit) Sorry, PhoneBook is obviously a production class. Rather, PhoneBookRepository would be the class/interface that you would create. It would be a production class/interface and you can use a simulated implementation for testing and a real implementation for production.
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1014
    
  47

I understand that you're just trying to write a one off program that will generate some output in your text file, the 10,000 directory entries, so here's some pointers to get what you have at least functional (if not elegant).

Pay attention to what Junilu is telling you about "get"ing a specific element from your extensions ArrayList to print rather than trying to print the whole thing. That will get your extension numbers printing.

You also have a second problem. Take a look at your code that professes to populate the extensions List with 10,000 unique int's. Are you sure you're putting 10,000 things in there? (Spoiler alert... you're not).

When you come to write your actual application I would certainly review the advice you've been given here with regards to taking a more OO approach to things. It will make your program, and your sanity, much better.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
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().


Okay, loads of answers but I've taken up this advice and have decided to make three methods for random surname, random initial and unique random extension... plus the main of course. randomSurname and randomInitial seem to work fine but I'm having difficulty with randomExtension. 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?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
PhoneBook, PhoneBookRepository, Contact, PhoneNumber, etc. Any of these sounds like a good class name. And there are many other possible names.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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?


From main:

call initializeExtensions() -- method to initialize a random sequence of extension numbers
call nextExtension() -- method that provides the next extension number

Here's the process for managing the extension numbers:
1. Add all possible extension numbers to a List, in sequence (no randomness yet)
2. Shuffle the list. You only need to do this once.
3. Get an iterator from the List.
4. Call the iterator.next() method to get the next number.

you could just use methods in your utility class or you could encapsulate better by creating another class as I suggested before. Either way works and is a matter of preference.
I will leave it up to you to organize your code around this process, if you choose to follow it. Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating random names and phone extensions, help please!