Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am working on a java zoo monitoring program and I keep getting an error

ID: 3712855 • Letter: H

Question

Hello, I am working on a java zoo monitoring program and I keep getting an error. can someone help?

here is my code...

/*
* 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 zoomonitor;

//ZooMonitor.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class ZooMonitor {

    //Create an instance of Scanner class
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        while (true) {

            //calling menu method
            int ch = menu();

            Scanner filereader = null;

            //For animals
            if (ch == 1) {
                //calling selectanimal
                int animal = selectanimal();
                String animalType = "";
                String animalName = "";
                try {

                    //opening animals.txt file
                    filereader = new Scanner(new File("C:\Users\arpec\Documents\NetBeansProjects\ZooMonitor\src\zoomonitor\animals.txt"));

                    switch (animal) {
                        case 1:
                            animalName = "Animal-Lion";
                            break;
                        case 2:
                            animalName = "Animal-Tiger";
                            break;
                        case 3:
                            animalName = "Animal-Bear";
                            break;
                        case 4:
                            animalName = "Animal-Giraffe";
                            break;
                    }

                    boolean repeat = true;
                    while (filereader.hasNextLine() && repeat) {
                        animalType = filereader.nextLine();
                        if (animalType.equals(animalName)) {
                            repeat = false;
                        }
                    }

                    String name = filereader.nextLine();
                    String age = filereader.nextLine();

                    String health = filereader.nextLine();
                    String feed = filereader.nextLine();

                    //checking if health or feed contains any concerns
                    if (health.contains("*****")) {
                        JOptionPane.showMessageDialog(null, health, "Warning : " + animalType, JOptionPane.INFORMATION_MESSAGE);
                    } else if (feed.contains("*****")) {
                        JOptionPane.showMessageDialog(null, feed, "Warning : " + animalType, JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "No health concerns");
                    }

                } catch (FileNotFoundException e) {
                    System.out.println(e.getMessage());
                }
            } //For habitats
            else if (ch == 2) {
                int animal = selecthabitat();
                String animalType = "";
                String animalName = "";
                try {

                    filereader = new Scanner(new File("C:\Users\arpec\Documents\NetBeansProjects\ZooMonitor\src\zoomonitor\habitats.txt"));

                    switch (animal) {
                        case 1:
                            animalName = "Habitat - Penguin";
                            break;
                        case 2:
                            animalName = "Habitat - Bird";
                            break;
                        case 3:
                            animalName = "Habitat - Aquarium";
                            break;
                    }

                    boolean repeat = true;
                    while (filereader.hasNextLine() && repeat) {
                        animalType = filereader.nextLine();
                        if (animalType.equals(animalName)) {
                            repeat = false;
                        }
                    }

                    String temperature = filereader.nextLine();
                    String food = filereader.nextLine();
                    String cleanliness = filereader.nextLine();

                    //checking if temperature,food or cleaniness contains any concerns
                    if (temperature.contains("*****")) {
                        JOptionPane.showMessageDialog(null, temperature, "Warning : " + animalType, JOptionPane.INFORMATION_MESSAGE);
                    } else if (food.contains("*****")) {
                        JOptionPane.showMessageDialog(null, food, "Warning : " + animalType, JOptionPane.INFORMATION_MESSAGE);
                    } else if (cleanliness.contains("*****")) {
                        JOptionPane.showMessageDialog(null, cleanliness, "Warning : " + animalType, JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "No health concerns");
                    }

                } catch (FileNotFoundException e) {
                    System.out.println(e.getMessage());
                }
            } else {
                //close the appliation
                System.out.println("Good Bye!");
                System.exit(0);

            }
        }
    }

//Method to select a habitat
    private static int selecthabitat() {
        System.out.println("1.Habitat - Penguin ");
        System.out.println("2.Habitat - Bird");
        System.out.println("3.Habitat - Aquarium");

        System.out.println("Enter your choice");
        int choice = Integer.parseInt(scanner.nextLine());

        return choice;

    }

//method to select an animal
    private static int selectanimal() {

        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");
        int choice = Integer.parseInt(scanner.nextLine());

        return choice;
    }

    private static int menu() {

        System.out.println("1.Monitor animal");
        System.out.println("2.Monitor habitat");
        System.out.println("3.Exit");

        int choice = Integer.parseInt(scanner.nextLine());

        return choice;

    }

}

my error is...

Exception in thread "main" java.util.NoSuchElementException: No line found
   at java.util.Scanner.nextLine(Scanner.java:1540)
   at zoomonitor.ZooMonitor.main(ZooMonitor.java:62)
C:UsersrpecAppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)

Thanks for your help

Explanation / Answer

The error, java.util.NoSuchElementException is often thrown in the context of a Scanner when you call nextLine() and there's no next line.

nextline() method doesn't return NULL if nothing is there, it throw an exception called java.util.NoSuchElementException.

It also depends on the formate of the text file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote