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
How do I get this program to only read specific sections of the animalsNew.txt. For example, I have set up several case statements. One option is to select lion, if that option is selected, I want the program to only read the section of the file that provides details on lions. Right now, the program that I have reads the entire file./*
* 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
*/
public static void main(String args[]) throws IOException {
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");
while (true) {
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() throws FileNotFoundException {
File zooAnimals = new File("animalsNew.txt");
Scanner lionFile = new Scanner(zooAnimals);
while(lionFile.hasNext())
{
String lionInfo = lionFile.nextLine();
System.out.println(lionInfo);
}
}
public static void readHabitatFile() {
File file = new File("habitatNew.txt");
try {
Scanner input = new Scanner(file);
String habitatName = input.nextLine();
System.out.printf("Request: %s", habitatName);
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s\n", ex);
}
}
public static void animalOptions() throws FileNotFoundException {
Scanner userChoiceAnimal = new Scanner(System.in);
int option = userChoiceAnimal.nextInt();
switch (option) {
case 1:
readAnimalFile();
break;
case 2:
System.out.println("Tigers");
break;
}
}
public static void habitatOptions() {
Scanner userChoiceHabitat = new Scanner(System.in);
int option = userChoiceHabitat.nextInt();
switch (option) {
case 1:
readHabitatFile();
break;
case 2:
System.out.println("Birds");
break;
case 3:
System.out.println("Aquarium");
break;
case 4:
System.out.println("Return to main menu");
break;
case 5:
System.out.println("Exit the program");
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