The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I have written a Java code for zoo monitoring but for some reason I keep getting error messages when I run it. I am using Java Net Beans IDE to build the program. Could someone please help me so that I can get this to work properly in Net Beans IDE?
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
//<editor-fold defaultstate="collapsed" desc="comment">
//</editor-fold>
public class ZooMonitoringSystem {
@SuppressWarnings("empty-statement")
public static void main(String[] args) throws IOException {
//main() function will allow user to choose something to monitor.
//regrdless of which is chosen the program will open a file, save each line
//to a list then search the list for details on while keeping track of wherevr it is
// on the list. It will the prompt the user for a specific animal and continue searching the
//list from where it left of to the end...while outputing the things it finds relevant
//while statement allows the user to return to main menu after completion
while (true) {
System.out.println("What would you like to monitor? ");
System.out.println("Enter 1 to monitor Animals ");
System.out.println("Enter 2 to monitor Habitats ");
System.out.println("Enter 3 to Exit");
int userInput = 0;
// scanner reads input from user
Scanner sc = new Scanner(System.in);
userInput = sc.nextInt();
// a variable to keep track of where we are searching in the file
int write = 0;
String animalName;
String habitatName;
//if the input from the user is the number 1
if (userInput == 1) {
System.out.println("List of Animals ");
//calls from animals file
Reader rd = new Reader("ANIMALS.txt");
//rd.get() will store all contents of animals file into a list
// the list's elements are each of the lines of the file
List<String> list = rd.get();
// a for loop to go through the list
for (String line : list) {
// if an element (line) in the list contains the words "Details on"
if (line.toLowerCase().contains("Details on".toLowerCase())) {
//increase the write variable that tracks the index of the list
write = write + 1;
//print out every line that has "details on" substring
System.out.println(line);
}
}
//allow user to input an animal's name for searching
System.out.println("Enter name of animal you would like to monitor: ");
animalName = sc.next();
for (String category : list.subList(write, list.size()))//specific range within the list (From where we stopped searching as tracked by the write variable, to the end of the list) {
//search for an animal in the list
{
if (category.toLowerCase().contains(animalName.toLowerCase())) {
write = list.indexOf(category);
// if an animal is found, then the next 5 elements in the list (next 5 lines in the file)
// are info about the animal
for (int i = write; i < write + 5; i++) {
//displays alert message if file contains ******--indicating abnormal detection
//check first if the line in the for loop contains ***
if (list.get(i).contains("*****")) {
JOptionPane.showMessageDialog(new JFrame(), list.get(i), "ALERT!!!", JOptionPane.ERROR_MESSAGE);
break;
} //if the line in the for loop doesn't contain a **** then print it printed
else {
System.out.println(list.get(i));
}
}
}
}
} else if (userInput == 2) {
System.out.println("List of Habitats ");
//calls from habitats file
Reader rd = new Reader("HABITATS.txt");
//rd.get() will store all contents of habitats file into a list
// the list's elements are each of the lines of the file
List<String> list = rd.get();
//loop through the whole list
for (String line : list) {
//search for "details on" in the list's elements
if (line.toLowerCase().contains("Details on".toLowerCase())) {
//maintain the position of the line being searched using the write variable
write = write + 1;
//print out every line that has "details on" substring
System.out.println(line);
}
}
System.out.println("Enter the name of a habitat you would like to monitor: ");
//ask user for a habitat name
habitatName = sc.next();
// search for a habitats beginning from where we left of our search as tracked by the write variable
//we use sublist() to separate already searched items
for (String res : list.subList(write, list.size())) {
//check if line contains a habitat name
if (res.toLowerCase().contains(habitatName.toLowerCase())) {
write = list.indexOf(res);
// if a habitat is found, then the next 4 elements in the list (next 4 lines in the file)
// are info about the habitat
for (int i = write; i < write + 4; i++) {
//check if the line to be printed contains "****"
if (list.get(i).contains("*****")) {
//raise Alert box
JOptionPane.showMessageDialog(new JFrame(), list.get(i), "ALERT!!!", JOptionPane.ERROR_MESSAGE);
break;
} //if the line in the for loop doesn't contain a **** then print it printed
else {
System.out.println(list.get(i));
}
}
}
}
} //if the user chooses the third choice
else if (userInput == 3) {
//close the scanner
sc.close();
//exit the system
System.exit(0);
}
}
}
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly