Home » Java » Using useDelimiter() to join substrings

Using useDelimiter() to join substrings

Posted by: admin October 21, 2018 Leave a comment

Questions:

My method readDataFromFile() can read text files like:

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

The first column is the ‘Type’ of animal, second column is ‘Species’ and third is ‘Name’.

Current Output:

Bird  Golden Eagle  < (Golden and Eagle count as different substrings).
    Mammal  Tiger Tommy
    Mammal  Lion Leo
    Bird  Parrot Polly
    Reptile  Cobra Colin
  • How would I use the useDelimiter method to make ‘Golden Eagle’ count as one species?

Current Code:

while(scanner.hasNextLine())
       {
       String type = scanner.next();
       String species = scanner.next();
       String name = scanner.next();
       System.out.println(type + "  " + species + " " + name);
       scanner.nextLine();

       addAnimal( new Animal(species, name, this) );
       }
Answers: