Javascript trouble with creating a zoo monitoring program Hi everyone! I\'ve tri
ID: 3575296 • Letter: J
Question
Javascript trouble with creating a zoo monitoring program
Hi everyone! I've tried everything imaginable to create this zoo monitoring code within netbeans but have been really struggling. Can anyone help? It is very basic coding and we have not learned about bufferedwriters or hash maps quite yet. However, we have learned about Fileinput streams and so on...Here is my code so far but I have not gotten around to finishing it. I know it has multiple errors and probably is very lengthy and confusing. I just need help revising and completing this in a basic java program.
package zoo;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.Scanner;
public class ZooMonitor {
private static Object scanner;
private static Object animals;
public static void main(String[] args) throws IOException {
FileInputStream AniMal = null;
Scanner inPT = null;
public static void main(String[] args) {
int opt;
while(true){
System.out.println("------ Menu ------");
System.out.println("1. Monitor an animal");
System.out.println("2. Monitor a habitat");
System.out.println("3. Exit");
opt = scanner.nextInt();
switch(opt){
case 1:
monAnimal();
break;
case 2:
monHabitat();
break;
case 3:
System.exit(0);
default:
System.out.println("Invalid Input");
}
}
}
public static void monAnimal() throws FileNotFoundException, IOException {
int opt ;
int num = 0;
System.out.println("------Animals------");
try {
String sCurrentLine;
FileInputStream aniMal = new FileInputStream("animals.txt") {
while ((sCurrentLine = aniMal.readLine()) != null) {
if(sCurrentLine.startsWith("Details")){
String animal=sCurrentLine.split(" ")[2];
System.out.println(++num+". "+animal);
}
}
}
FileInputStream aniMal = new FileInputStream("animals.txt");
System.out.println("Enter Animal Option: ");
opt = scanner.nextInt();
String selectAnimal = animals.get(opt);
if(selectAnimal!=null); {
while (sCurrentLine = FileInputStream.readLine()) != null) {
if(sCurrentLine.startsWith("Animal")&&(sCurrentLine.split(" ")[2].equalsIgnoreCase(selectAnimal)||selectAnimal.startsWith(sCurrentLine.split(" ")[2].toLowerCase()))){
}
}
}
private static void monHabitat() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Also here is the rubric for what the project is:
Create monitoring system that does the following:
Asks a user if they want to monitor an animal, monitor a habitat, or exit
Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file o Asks the user to enter one of the options
Displays the monitoring information by finding the appropriate section in the file
Separates sections by the category and selection (such as “Animal - Lion” or “Habitat - Penguin”)
Uses a dialog box to alert the zookeeper if the monitor detects something out of the normal range (These will be denoted in the files by a new line starting with *****. Do not display the asterisks in the dialog.)
Allows a user to return to the original options
************************************************
ANIMALS.TXT file
*********************************************
HABITATS.TXT file
Explanation / Answer
There were lot of errors in your code. I have corrected the same. Please include the required logic in the animal and habitat function for this to be a complete program.
package temp;
// Scanner is sufficient to read from File and from the user.
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class ZooMonitor {
public static void main(String[] args) throws Exception {
int opt;
// Creating a Scanner object for user input.
Scanner sc = new Scanner(System.in);
ZooMonitor zm = new ZooMonitor();
while(true){
// Menu part.
System.out.println("Choose the option from the MENU");
System.out.println("------ Menu ------");
System.out.println("1. Monitor an animal");
System.out.println("2. Monitor a habitat");
System.out.println("3. Exit");
// Based on user input, call the respective functions.
// For invalid case, exit the program
opt = sc.nextInt();
switch(opt){
case 1:
zm.monAnimal();
break;
case 2:
zm.monHabitat();
break;
default:
System.out.println("Invalid Input");
sc.close();
break;
}
}
}
// habitat function.
private void monHabitat() throws FileNotFoundException {
int opt ;
int num = 0;
System.out.println("------Habitat------");
// Creating a file object to read from the file
File file = new File("animals.txt");
// Through scanner, I am reading the file and displaying the contents
Scanner sc = new Scanner(file);
while(sc.hasNext()){
String str = sc.nextLine();
System.out.println(str);
}
sc.close();
}
// animal function.
private void monAnimal()throws FileNotFoundException {
int opt ;
int num = 0;
System.out.println("------Animals------");
// Creating a file object to read from the file
File file = new File("habitat.txt");
// Through scanner, I am reading the file and displaying the contents
Scanner sc = new Scanner(file);
while(sc.hasNext()){
String str = sc.nextLine();
System.out.println(str);
}
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.