Question
I am writing a program to monitor animals and habitats in a zoo, reading from 2 different .txt files. The animals portion of my code
I am writing a program to monitor animals and habitats in a zoo, reading from 2 different .txt files. The animals portion of my code is working perfectly, but when I try to execute the habitats portion, I get errors. Below is both my code and recieved errors:
import java.util.Scanner; import java.io.FileNotFoundException;
public class ZooMonitor { static fileread fr = new fileread();
private static Scanner scnr = new Scanner(System.in); public static void main(String[] args)throws FileNotFoundException { while(true){ int choice = menu(); if(choice == 1){ // Select animal int animal = animal_select(); String Type = ""; String Name = "";
switch (animal){ case 1: Name = "Animal - Lion"; break; case 2: Name = "Animal - Tiger"; break; case 3: Name = "Animal - Bear"; break; case 4: Name = "Animal - Giraffe"; break; case 5: } //read requested information from file
fileread.read_animal(Name); } else if(choice == 2){
//select the habitat
int animal = habitat_select(); String Type = ""; String Name = "";
switch (animal){ case 1: Name = "Habitat - Penguin"; break; case 2: Name = "Habitat - Bird"; break; case 3: Name = "Habitat - Aquarium"; break; case 4: }
//read requested information from file
fileread.read_habitat(Name); } else{ System.out.println("Thank You!"); System.exit(0); } } }
private static int habitat_select(){ //Display habitat options
System.out.println("1.Habitat - Penguin "); System.out.println("2.Habitat - Bird"); System.out.println("3.Habitat - Aquarium"); System.out.println("Enter your choice, 1-3"); int choice = scnr.nextInt(); return choice; }
private static int animal_select(){ //Display animal options
System.out.println("1.Animal - Lion"); System.out.println("2.Animal - Tiger"); System.out.println("3.Animal - Bear"); System.out.println("4.Animal - Giraffe"); System.out.println("Enter your choice, 1-4"); int choice = scnr.nextInt();
return choice; } private static int menu(){ //Main menu
System.out.println("1.Monitor animals"); System.out.println("2.Monitor habitats"); System.out.println("3.Exit"); int choice = scnr.nextInt();
return choice; }
}
//Second .java file
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane;
public class fileread { private static Scanner fileread;
// constructor public fileread(){ fileread = null; } public static void read_animal(String Name) throws FileNotFoundException{ //read from the animals.txt file fileread = new Scanner (new File("C:\\Users\\Debbie and Drew\\Desktop\\animals.txt")); boolean cont = true; String Type = ""; //read the file until match found while(fileread.hasNextLine() &&cont){ Type=fileread.nextLine(); if(Type.equals(Name)) cont = false; } //read file data String animal_name = fileread.nextLine(); String animal_age = fileread.nextLine(); String animal_health = fileread.nextLine(); String animal_feed = fileread.nextLine(); //prints information System.out.println(animal_name); System.out.println(animal_age); //if warning present, display if(animal_health.contains("*****")) { JOptionPane.showMessageDialog(null, animal_health.substring(5,animal_health.length()), "Warning : " + Type, JOptionPane.INFORMATION_MESSAGE); } else { System.out.println(animal_health); }
if(animal_feed.contains("*****")) { JOptionPane.showMessageDialog(null, animal_feed.substring(5,animal_feed.length()), "Warning : " + Type, JOptionPane.INFORMATION_MESSAGE); } else { System.out.println(animal_feed); } } public static void read_habitat(String Name) throws FileNotFoundException{ //read from the habitats.txt file fileread = new Scanner (new File("C:\\Users\\Debbie and Drew\\Desktop\\habitats.txt")); boolean cont = true; String Type = "";
//read file until match found while(fileread.hasNextLine() &&cont){ Type = fileread.nextLine(); if(Type.equals(Name)) cont=false; }
//read information String temperature = fileread.nextLine(); String food = fileread.nextLine(); String cleanliness = fileread.nextLine();
//if warning present, display if(temperature.contains("*****")) { JOptionPane.showMessageDialog(null, temperature.substring(5,temperature.length()), "Warning : " + Type, JOptionPane.INFORMATION_MESSAGE); } else { System.out.println(temperature); }
if(food.contains("*****")) { JOptionPane.showMessageDialog(null, food.substring(5,food.length()), "Warning : " + Type, JOptionPane.INFORMATION_MESSAGE); } else { System.out.println(food); }
if(cleanliness.contains("*****")) { JOptionPane.showMessageDialog(null, cleanliness.substring(5,cleanliness.length()), "Warning : " + Type, JOptionPane.INFORMATION_MESSAGE); } else { System.out.println(cleanliness); } } } This results in the following:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at fileread.read_habitat(fileread.java:65) at ZooMonitor.main(ZooMonitor.java:61)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started