0

I have a method called readDataFromFiles() which reads text files such as this:

Bird    Golden Eagle    Eddie
Mammal  Tiger   Tommy
Mammal  Lion    Leo
Bird    Parrot  Polly
Reptile Cobra   Colin

I have seem to have done that successfully however how do I split the 3 Substrings and create an animal object to the zoo?

Also ignoring the first substring "Bird" since it would make it too complicated because the first line would have 4 substrings.

Any advice or help would be great!

  • I believe I HAVE to use a scanner method and NOT a split() method!

Question:

Extract the three substrings and then create and add an Animal object to the zoo

Zoo Class:

public class MyZoo
{
   private String zooId;
   private int nextAnimalIdNumber;
   private TreeMap<String, Animal> animals;
   private Animal animal;

   public MyZoo(String zooId)
   {
      this.zooId = zooId.trim().substring(0,3).toUpperCase();
      nextAnimalIdNumber = 0;
      animals = new TreeMap<String, Animal>();
   }

   public String allocateId()
   {
      nextAnimalIdNumber++;
      String s = Integer.toString(nextAnimalIdNumber);
      while ( s.length()<6 )
         s = "0" + s;
      return zooId + "_" +  s;
   }

   public void addAnimal(Animal animal)
   {
      animals.put(animal.getName(), animal);
      this.animal = animal;
   }

   public void readDataFromFile() throws FileNotFoundException
   {
      int noOfAnimalsRead = 0;

      String fileName = null;

      JFrame mainWindow = new JFrame();
      FileDialog fileDialogBox = new FileDialog(mainWindow, "Open", FileDialog.LOAD);
      fileDialogBox.setDirectory("."); 
      fileDialogBox.setVisible(true);

      fileName = fileDialogBox.getFile();
      String directoryPath = fileDialogBox.getDirectory();

      File dataFile = new File (fileName);
      Scanner scanner = new Scanner(dataFile);
      //System.out.println("The selected file is " + fileName);

      scanner.next();
      while(scanner.hasNextLine())
      {
       String name = scanner.nextLine();
       String[] animals = name.split("\\s+");
       System.out.println(name);
      }
    }

Animal Class:

public class Animal
{
   private String id;
   private String species;
   private String name;
   public Animal(String species, String name, MyZoo owner)
   {
      id = owner.allocateId();
      this.species = species;
      this.name  = name;
   }

   public String getId()
   {
      return id;
   }

   public String getName()
   {
      return name;
   }

   public String getSpecies()
   {
      return species;
   }

   public String toString()
   {
      return id + "  " + name + ": a " + species;
   }
}
  • so... homework? Nice. – nicomp Jul 12 '18 at 19:30
  • so are you not allowed to use the `split()` method? – user3170251 Jul 12 '18 at 19:33
  • i used the split method myself but then i was told that it can be done using scanners alone. maybe using split() would over complicate it.. not sure – Melissa Stewart Jul 12 '18 at 19:34
  • This might help: https://stackoverflow.com/questions/28766377/how-do-i-use-a-delimiter-in-java-scanner – SHN Jul 12 '18 at 19:37
  • 1
    you can use `Scanner.next()` to grab each word, but in my opinion getting the whole line then calling `split()` makes more intuitive sense. Please take a look at https://stackoverflow.com/questions/4574041/read-next-word-in-java – user3170251 Jul 12 '18 at 19:39
  • Checked both links out, however i'm still not quite sure how i would even implement it to my own code. The text files im reading have 3 pieces of data on 1 line.. – Melissa Stewart Jul 12 '18 at 19:56
  • Hi @MelissaStewart, since I answered your first question and I recommended `String.split`, I wonder who told you to user `Scanner`. I would use what works and unless you want to explore alternatives, the split approach is fine. –  Jul 12 '18 at 19:59
  • @MelissaStewart check out this quick example (https://ideone.com/WYCAT2) I made. I called `sc.next()` 3 times to get the 3 animal properties of the sample input. I agree with Lutz though, stick with `split` as its the simpler solution – user3170251 Jul 12 '18 at 20:08

0 Answers0