Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. Counting animals in a zoo All similar animals in a zoo share the same cage

ID: 3856233 • Letter: J

Question

 JAVA. Counting animals in a zoo All similar animals in a zoo share the same cage. For example, south china tiger and javan tiger are in the same cage, and we only want to know there are two tigers.  Given an integer K, followed by K lines describing animals, count and output in alphabetical order the animals in the zoo.   The input data file includes multiple cases. Each case contains a number K (0<=K<=500), followed by K lines of animals with at least one word on each line, and the last word describing the kind of animal. For example, Caspian tiger is a tiger (obviously). The input is terminated by a 0. Output: For each test case, count and output the number for each animal type in alphabetic order: animal type followed by : and the number of that type animal.  Sample input                                    Sample output 5                                                  List 1: Bengal tiger                                       bear : 2 Brown bear                                         tiger : 3 Sun bear                                           List 2: South China tiger                                  penguin : 2 Bali tiger 2 King penguin Fiordland penguin 0 

Explanation / Answer

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

public class Testtt

{

    public static void main(String args[])

    {

        try {

        writeToFile();

        }

        catch(Exception e)

        {

            System.out.println("exception "+e);

        }

    }

   

    public static void writeToFile() throws Exception

    {

String filePathAnimalsList = "C:\Answers\animalsList.txt";

String filePathAnimalsReport = "C:\Answers\animalsReport.txt";

FileReader fileReader = new FileReader(filePathAnimalsList);

            BufferedReader readList = new BufferedReader(fileReader);

           

            FileWriter fw = new FileWriter(filePathAnimalsReport);

            BufferedWriter writeReport = new BufferedWriter(fw);

           

           // StreamWriter writeReport = new StreamWriter(filePathAnimalsReport);

            String dataAnimals = readList.readLine();

            int cnt = 0;

            int list = 1;

                        

            String animal = "";

            int j = 0;

            String[] animalsList;

            while (dataAnimals != null)

            {

                int temp = Integer.parseInt(dataAnimals.trim());  

                String[] dataList = new String[temp];                            

                if (temp > 0)

                {                                     

                    String animals = "";

                    for (int k = 0; k < temp; k++)

                    {

                        dataList[k] = readList.readLine();

                        int hh = (dataList[k].trim()).lastIndexOf(' ');

                        animal = dataList[k].substring(hh).trim();

                        if (!animals.contains(animal))

                        {

                            animals = animals + "," + animal;

                        }

                    }

                    writeReport.write("List:" + list);               

                    animalsList = animals.trim().split(",");

                    for (int z = 0; z < animalsList.length; z++)

                    {

                        for (int k = 0; k < dataList.length; k++)

                        {

                            int hh = (dataList[k].trim()).lastIndexOf(' ');

                            animal = dataList[k].substring(hh).trim();

                            if (dataList[k].contains(animalsList[z]))

                            {

                                cnt++;

                            }

                        }

                        writeReport.write("   " + animalsList[z] + ":" + cnt);

                       

                        cnt = 0;

                    }

                    list++;

                }

                dataAnimals = readList.readLine();             

            }

            readList.close();

            fileReader.close();

            writeReport.close();

        }

}