Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

JAVA code.

Hi- I need some help with JAVA code.

  • When I select 1 for animal, it’ll ask for which animal. If I select 1 to 3, it works but if I select animal #4 it throws my index error even though it’s within the limit
  • When I select 2 for habitat, it’ll ask for which habitat. Whatever number I select here, it’ll actually end up listing the animal for that index and not the habitat. If I select habitat #3, it’ll through my index error as well even though it’s within the limit

I also need to implement a pop up alert whenever the animal/habit with ***** are selected. The alert needs to display the message without the *****

Monitor.JAVA

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

/**

 * Monitor

 * @

 */

public class Monitor {

  static ArrayList<Animal> animals = new ArrayList<>();

  static ArrayList<Habitat> habitats = new ArrayList<>();

  public static void main(String[] args) {

    readAnimals(“animals.txt”);

    readHabitats(“habitats.txt”);

    Scanner input = new Scanner(System.in);

    int choice;

    while(true) {

      System.out.println(“Please select what you would like to view:nEnter 1 to monitor AnimalnEnter 2 to monitor HabitatnEnter 3 to Exit”);     

      choice = input.nextInt();

      if(choice == 1) {

        System.out.println(“Displaying animals…”);

        for(int i = 0; i < animals.size(); i++) {

          System.out.println((i+1) + “. ” + animals.get(i).getCategory());

        }

        System.out.print(“Choose animal index?n”);

        int index = input.nextInt();

        if(index >= animals.size()) {

          System.out.println(“error: invalid index!!”);

        } else {

          if(animals.get(index).getHealthConcerns().startsWith(“*****”)) {  //MODIFIED

            Animal anml = animals.get(index);  //MODIFIED

            String str = “Animal – ” + anml.getCategory() + “n”

                + “Name: ” + anml.getName() + “n”

                + “Age: ” + anml.getAge() + “n”

                + “Health concerns: ” + anml.getHealthConcerns().substring(anml.getHealthConcerns().indexOf(“: “) + 2) +

                “nFeeding schedule: ” + anml.getFeedingSchedule().substring(anml.getFeedingSchedule().indexOf(“: “) + 2);

            JOptionPane.showMessageDialog(null, str);

          } else {

            System.out.println(animals.get(index)); //MODIFIED

          }

        }

      } else if(choice == 2) {

        System.out.println(“Displaying habitats…”);

        for(int i = 0; i < habitats.size(); i++) {

          System.out.println((i+1) + “. ” + habitats.get(i).getCategory());           

        }

        System.out.print(“Choose habitat index?n”);

        int index = input.nextInt();

        if(index >= habitats.size()) {

          System.out.println(“error: invalid index!!”);

        } else {

          if(habitats.get(index).getFoodSource().startsWith(“*****”)) { //MODIFIED

            Habitat hab = habitats.get(index); //MODIFIED

            String str = “Habitat – ” + hab.getCategory() + 

                “n” + hab.getTemperature() + “n”

                + “Cleanliness: ” + hab.getCleanliness().substring(hab.getCleanliness().indexOf(“:”) + 2)

                + “nFood Source: ” + hab.getFoodSource().substring(hab.getFoodSource().indexOf(“:”) + 2);

            JOptionPane.showMessageDialog(null, str);

          } else {

            System.out.println(animals.get(index)); //MODIFIED

          }

        }

      } else if(choice == 3) {

        break;

      } else {

        System.out.println(“Invalid choice”);

      }

    }     

  }  

  public static void readAnimals(String filename) {

    try {

      Scanner fileIn = new Scanner(new File(filename));

      while(fileIn.hasNextLine()) {

        String line = fileIn.nextLine();

        if(line.startsWith(“Animal – “)) {

          String category = line.substring(line.indexOf(“-“)+2);      

          line = fileIn.nextLine();

          String name = line.substring(line.indexOf(“:”)+2);

          line = fileIn.nextLine();

          int age = Integer.parseInt(line.substring(line.indexOf(“:”)+2));

          line = fileIn.nextLine();

          String concerns = line;//.substring(line.indexOf(“:”)+2);

          line = fileIn.nextLine();

          String feedingScehdule = line;//.substring(line.indexOf(“:”)+2);

          Animal animal = new Animal(category, name, age, concerns, feedingScehdule);

          animals.add(animal);

          //System.out.println(“adding…n” + animal);

        }

      }

    } catch (FileNotFoundException ex) {

      Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);

    }

  }

  public static void readHabitats(String filename) {

    try {

      Scanner fileIn = new Scanner(new File(filename));

      while(fileIn.hasNextLine()) {

        String line = fileIn.nextLine();

        if(line.startsWith(“Habitat – “)) {

          String category = line.substring(line.indexOf(“-“)+1);           

          line = fileIn.nextLine();

          String temp = line;

          line = fileIn.nextLine();

          String foodSource = line;

          line = fileIn.nextLine();

          String cleanliness = line;

          Habitat habitat = new Habitat(category, temp, foodSource, cleanliness);

          habitats.add(habitat);

          //System.out.println(“adding…n” + habitat);

        }

      }

    } catch (FileNotFoundException ex) {

      Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);

    }

  }

}

Habitat.JAVA

public class Habitat {

  private String category;

  private String temperature;

  private String foodSource;

  private String cleanliness;

  public Habitat(String category, String temperature, String foodSource, String cleanliness) {

    this.category = category;

    this.temperature = temperature;

    this.foodSource = foodSource;

    this.cleanliness = cleanliness;

  }

  public String getCategory() {

    return category;

  }

  public void setCategory(String category) {

    this.category = category;

  }

  public String getTemperature() {

    return temperature;

  }

  public void setTemperature(String temperature) {

    this.temperature = temperature;

  }

  public String getFoodSource() {

    return foodSource;

  }

  public void setFoodSource(String foodSource) {

    this.foodSource = foodSource;

  }

  public String getCleanliness() {

    return cleanliness;

  }

  public void setCleanliness(String cleanliness) {

    this.cleanliness = cleanliness;

  }

  public String toString() {

    String str = “Habitat – ” + category + 

        “n” + temperature +

        “n” + foodSource +

        “n” + cleanliness;

    return str;

  }

}

Animal.JAVA

public class Animal {

  private String category;

  private String name;

  private int age;

  private String healthConcerns;

  private String feedingSchedule;

  public Animal(String category, String name, int age, String healthConcerns, String feedingSchedule) {

    this.category = category;

    this.name = name;

    this.age = age;

    this.healthConcerns = healthConcerns;

    this.feedingSchedule = feedingSchedule;

  }

  public String getCategory() {

    return category;

  }

  public void setCategory(String category) {

    this.category = category;

  }

  public String getName() {

    return name;

  }

  public void setName(String name) {

    this.name = name;

  }

  public int getAge() {

    return age;

  }

  public void setAge(int age) {

    this.age = age;

  }

  public String getHealthConcerns() {

    return healthConcerns;

  }

  public void setHealthConcerns(String healthConcerns) {

    this.healthConcerns = healthConcerns;

  }

  public String getFeedingSchedule() {

    return feedingSchedule;

  }

  public void setFeedingSchedule(String feedingSchedule) {

    this.feedingSchedule = feedingSchedule;

  }

  @Override

  public String toString() {

    String str = “Animal – ” + category + 

        “n” + “Name: ” + name +

        “n” + “Age: ” + age +

        “n” + healthConcerns +

        “n” + feedingSchedule;

    return str;

  }

}

habitats.TXT

Details on penguin habitat

Details on bird house

Details on aquarium

Habitat – Penguin

Temperature: Freezing

*****Food source: Fish in water running low

Cleanliness: Passed

Habitat – Bird

Temperature: Moderate

Food source: Natural from environment

Cleanliness: Passed

Habitat – Aquarium

Temperature: Varies with output temperature

Food source: Added daily

*****Cleanliness: Needs cleaning from algae

animals.TXT

Details on lions

Details on tigers

Details on bears

Details on giraffes

Animal – Lion

Name: Leo

Age: 5

*****Health concerns: Cut on left front paw

Feeding schedule: Twice daily

Animal – Tiger

Name: Maj

Age: 15

Health concerns: None

Feeding schedule: 3x daily

Animal – Bear

Name: Baloo

Age: 1

Health concerns: None

*****Feeding schedule: None on record

Animal – Giraffe

Name: Spots

Age: 12

Health concerns: None

Feeding schedule: Grazing

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"