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 am trying to understand what I am doing wrong in this java program. I have a switch statement that reads from a file. For example, the first file works so that I can choose option 3 and only get information that involves bear. The second file (habitat), I set up like the first. I have a delimiter setup and its range is new lines {2,} however it's not reading from the habitat file right. It reads the whole file, if I select case 1, and errors if I select any other option.
My second issue is nesting a if statement inside the while loop. Both files have a statement, that should trigger a pop-up warning. For example, if the program reads "*****," it should trigger the pop-up warning and display the statement attached to the asterisk ie, "Health Concern." The error that I get is I am using .equals on an incompatible type. But if I am comparing strings, .equals is preferred over ==./*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package zookeepermonitoringsystem;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
/**
*
* @author Louis
*/
public class ZooKeeperMonitoringSystem {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
int[] animalArray = new int[4];
int[] habitatArray = new int[4];
public static void main(String args[]) throws IOException {
while (true) {
System.out.println("Welcome to the San Diego Zoo Animal "
+ "Monitoring System");
System.out.println("Please choose one of the following options: ");
System.out.println("1. Monitor an Animal");
System.out.println("2. Monitor a Habitat");
System.out.println("3. Exit");
Scanner mainMenuOptions = new Scanner(System.in);
int option = mainMenuOptions.nextInt();
switch (option) {
case 1:
System.out.println("Select one animal for monitoring: ");
System.out.println("1. Lion");
System.out.println("2. Tiger");
System.out.println("3. Bear");
System.out.println("4. Giraffe");
System.out.println("5. Return to main menu");
animalOptions();
break;
case 2:
System.out.println("Select one habitat to monitor: ");
System.out.println("1. Penguin Habitat");
System.out.println("2. Bird House");
System.out.println("3. Aquarium");
System.out.println("4. Return to main menu");
habitatOptions();
break;
case 3:
System.out.println("Exit");
break;
default:
System.out.println("Please select an option from "
+ "the main menu.");
}
}
}
public static void readAnimalFile(int num) throws FileNotFoundException {
File zooAnimals = new File("animalsNew2.txt");
Scanner animalFile = new Scanner(zooAnimals);
animalFile.useDelimiter("\n{2,}");
for (int i = 1; i < num; i++) {
animalFile.next();
}
String animalInfo = animalFile.next();
System.out.println(animalInfo);
}
public static void animalOptions() throws FileNotFoundException {
Scanner userChoiceAnimal = new Scanner(System.in);
int option = userChoiceAnimal.nextInt();
switch (option) {
case 1:
case 2:
case 3:
case 4:
readAnimalFile(option);
break;
default:
System.out.print("Please choose a valid option.");
break;
}
}
public static void readHabitatFile(int num) throws FileNotFoundException {
File zooHabitat = new File("habitatNew.txt");
Scanner habitatFile = new Scanner(zooHabitat);
habitatFile.useDelimiter("\n{2,}");
for (int i = 1; i < num; i++) {
habitatFile.next();
}
String habitatInfo = habitatFile.next();
System.out.println(habitatInfo);
}
public static void habitatOptions() throws FileNotFoundException {
Scanner userChoiceHabitat = new Scanner(System.in);
int option = userChoiceHabitat.nextInt();
switch (option) {
case 1:
case 2:
case 3:
readHabitatFile(option);
break;
default:
System.out.print("Please choose a valid option.");
break;
}
}
}
----------- ----------- 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