1

I'm still very new to java. I am trying to create an array of objects from a text file. The text file has a list of names, and using these names, I'm trying to create objects. This is the method I've created to create the objects from the text file input. It gives an error when compiled. I'm not sure where I've done wrong.

   public boolean createObjects(PersonNames2[] person) throws Exception
   {
       boolean found = false;
       int position = 0;
       if(canCreateObjects() == true)
       {
           for(int i = 0; i < persons.length && !found; i++)
           {
               if(persons[i] == null)
               {
                   position = i;
                   found = true;
               }
           }
           Scanner reader = new Scanner(file);
           while(reader.hasNext())
           {
               person[position] = new PersonNames2();
               position++;
           }
           reader.close();
           return true;
       }
       return false;
   }
5
  • Share your compilation error.... Commented Sep 14, 2015 at 7:11
  • 2
    parameter is person and your are using persons Commented Sep 14, 2015 at 7:13
  • I get this error PersonNames2.java:27: error: array dimension missing PersonNames person[] = new Person[]; ^ 1 error Commented Sep 14, 2015 at 7:13
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Lol....welcome back....have not seen u here for sometime...im also not so active:p Commented Sep 14, 2015 at 7:13
  • @Rustam good pick with a naked eye Commented Sep 14, 2015 at 7:14

3 Answers 3

2

error: array dimension missing PersonNames person[] = new Person[];

That's clearly telling that you are failed to give the size of your array.

You need to write

PersonNames person[] = new Person[size]; // For ex : 10 or any X

Array's are fixed in size and you need to tell the size of it while declaring/initializing it self.

Update:

Since you are reading data from a file and no idea about the length of array, better to choose ArrayList instead of array. Size of the ArrayList increases over the time you add elements to it.

Sign up to request clarification or add additional context in comments.

4 Comments

If I were to create a an array with a set size i would just PersonNames person = new Person[5]. How can I go about doing this if I'm using the no of lines in the file as the size
@MichelleAshwini Updated my answer :) Hope that helps
@SURESHATTA That makes sense. I'm trying that out now. Will get back on it.
I thought you were passing array as argument in function boolean createObjects(PersonNames2[] person) so why need to initialize ???
0

If you are new in Java, than this tiny GitHub project could be a nice set of hints for you.

Comments

0

I've managed to get this working. Thanks! Here is the working code.

    public static List<Person> loadPersons(String path) throws Exception
    {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        List<Person> persons = new ArrayList<Person>();

        while((line = reader.readLine()) != null)
        {
            System.out.println("Adding " +line);
            persons.add(new Person(line));
        }

        return persons; 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.