Question: I am working on an assignment for my java programming class that requires me to make a rudimentary monitoring program for a zoo that allows

I am working on an assignment for my java programming class that requires me to make a rudimentary monitoring program for a zoo that allows a zookeeper to monitor both animals and habitats while simultaniously informing them of any abnormalities for animals or habitat located in .txt files contained within the program. This has to be done with two different classes, one of which has been supplied. I have most of my code down pat, I just can't seem to get to past my method call to my secondary class. Here is my code:

************************************************************************

package monitoring;

import java.util.Scanner;

public class Monitoring { private static Scanner usrch = new Scanner(System.in);

public static void main(String[] args) { AnimalsHabitat methodCall = new AnimalsHabitat(); try { int userChoice = mainMenu(); switch(userChoice) { case 1: methodCall.askForWhichDetails("animals.txt"); break; case 2: methodCall.askForWhichDetails("habitats.txt"); break; case 3: System.out.println("Exiting program."); System.exit(0); break; default: int loopError = 0; while (loopError < 3) { loopError++; if (loopError == 3) { System.out.println("Error in program loop, exiting program."); System.exit(0); } } } } catch (Exception e) { System.out.println("Error:> " + e.getMessage()); } } public static int mainMenu() { System.out.println("Welcome to the Zoo Habitat and Animal Monitoring System (ZHAM)."); System.out.println("Please select what you would like to do."); System.out.println(""); System.out.println("(1) - Animal Monitoring"); System.out.println("(2) - Habitat Monitoring"); System.out.println("(3) - Exit the program"); int userChoice = Integer.parseInt(usrch.nextLine()); return userChoice; } } ************************************************************************

and here is the code from the secondary cclass that I was provided:

************************************************************************

package monitoring;

import java.io.FileInputStream; import java.io.IOException; import java.util.*; import javax.swing.JOptionPane;

public class AnimalsHabitat {

private String filePath; final private Scanner scnr;

public AnimalsHabitat() { /* Note: If you place your .txt datafiles on the same level as your .java files are at then you will not need to specify a 'filePath' like: filePath = "C:\\Users\\...\\Monitor\\"; */ filePath = ""; scnr = new Scanner(System.in); }

public void askForWhichDetails(String fileName) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object

String textLine = null; ArrayList aList1 = new ArrayList();

int i = 0; int option = 0;

boolean bailOut = false;

// Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); inFS = new Scanner(fileByteStream);

while (inFS.hasNextLine() && bailOut == false) { textLine = inFS.nextLine();

if (textLine.contains("Details")) { i += 1; System.out.println(i + ". " + textLine);

ArrayList aList2 = new ArrayList();

for (String retval : textLine.split(" ")) { aList2.add(retval); }

String str = aList2.remove(2).toString(); aList1.add(str); } else { System.out.print("Enter selection: "); option = scnr.nextInt();

System.out.println("");

if (option <= i) { String detailOption = aList1.remove(option - 1).toString(); showData(fileName, detailOption);

bailOut = true; }

break; } }

// Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails }

public void showData(String fileName, String detailOption) throws IOException { FileInputStream fileByteStream = null; // File input stream Scanner inFS = null; // Scanner object

String textLine = null; String lcTextLine = null; String alertMessage = "*****";

int lcStr1Len = fileName.length(); String lcStr1 = fileName.toLowerCase().substring(0, lcStr1Len - 1);

int lcStr2Len = detailOption.length(); String lcStr2 = detailOption.toLowerCase().substring(0, lcStr2Len - 1);

boolean bailOut = false;

// Try to open file fileByteStream = new FileInputStream(filePath + fileName + ".txt"); inFS = new Scanner(fileByteStream);

while (inFS.hasNextLine() && bailOut == false) { textLine = inFS.nextLine(); lcTextLine = textLine.toLowerCase();

if (lcTextLine.contains(lcStr1) && lcTextLine.contains(lcStr2)) { do { System.out.println(textLine);

textLine = inFS.nextLine(); if (textLine.isEmpty()) { bailOut = true; } if (textLine.contains(alertMessage)) { JOptionPane.showMessageDialog(null, textLine.substring(5)); }

} while (inFS.hasNextLine() && bailOut == false); } }

// Done with file, so try to close it fileByteStream.close(); // close() may throw IOException if fails } }

************************************************************************

I've been trying to troubleshoot this for hours, with little success. At first I was getting an error about my call itself, stating that I had a non-static call trying to call a static method, so I moved my instance for the secondary class to the line below my main class. Now I am met with a new error entirely:

************************************************************************

run:

Exception in thread "main" java.lang.NoClassDefFoundError: monitoring/AnimalsHabitat (wrong name: AnimalsHabitat)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)

at java.net.URLClassLoader.access$100(URLClassLoader.java:73)

at java.net.URLClassLoader$1.run(URLClassLoader.java:368)

at java.net.URLClassLoader$1.run(URLClassLoader.java:362)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:361)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

at monitoring.Monitoring.main(Monitoring.java:9)

C:\Users\Vampi\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1

BUILD FAILED (total time: 0 seconds)

************************************************************************

I am completely unsure of how to go about fixing this, as the secondary class is calld AnimalsHabitat and I'm racking my brain trying to figure out how I can fix this.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!