Click here to Skip to main content
15,395,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
public class AddressBook {
    private String[] firstNames = new String[1000];
    int firstNameCount = 0;
    private String [] lastNames = new String[1000];
    int lastNameCount = 0;
    private String[] addresses = new String[1000];
    int addressCount = 0;

    public void readFromFile(String filename) throws Exception {
         Scanner sc = new Scanner (new File(filename));
         sc.useDelimiter(",");

         int bitCount = 0;
         while(sc.hasNext()) {
             String bit = sc.next();
             switch(bitCount++) {
                 case 0:
                      fistNames[firstNameCount++] = bit;
                      break;
                 case 1:
                      lastNames[lastNameCount++] = bit;

                 case 2:
                      addresses[addressCount++] = bit;
             }
             if (bitCount > 2)
                 bitCount = 0;
         }


What I have tried:

3 wanted to know  worst problems with this implementation, in your opinion.
Posted
Updated 10-Jul-21 7:23am
Comments
Dave Kreskowiak 10-Jul-21 11:32am
   
This reeks of a homework question.

Well, it's not C. Or Python. Or Javascript. And that's three already ...

After that ... it looks like student grade code - which isn't a compliment; it uses magic numbers that aren't explained; it's never head of the modulus operator; the method name isn't reflective of what it's actually doing; it's undocumented; it assumes a max size for file data; it has nothing linking dtaa items together; it ...
Ah. Too many ...

In a code review, that would get a single word description.
   
Comments
johnathan indigo 10-Jul-21 10:19am
   
a better implementation, keeping the public interface the same?
OriginalGriff 10-Jul-21 11:36am
   
Throw it in the bin and start again, this time thinking first about your data and what you are trying to do? :laugh:

That's what I meant by "Student grade code" - it looks thrown together without any planning at all, or thought given to how you use the data.

Think about it: Ignore that your file format is badly designed, and think about what you might have to do with the data once you have read it. Then plan your classes to make that clear and simple. Then think about loading data from the file!

Literally, there is nothing there I would keep unchanged :laugh:
Rick York 10-Jul-21 21:13pm
   
Yes, I am sure there is one.
In my mind the worst problem is, that you want to cheat by letting other help you to solve your homework.
Bad is ofcourse the use of the "magic numbers" 2 and 1000 and no check for the file input.
I personally hate to name a string "bit".
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900