3
Scanning for and filter out chars
steehs1532 (2)

I have this code that is supposed to look for "*" and get rid of those asterisks and post an alert. My code works for every case except for the aquarium case in my habitat file. Is it some scanning exception for being at the end of a file?

Code: https://repl.it/@steehs1532/project

Example: What would you like to monitor? Please type 'animal', 'habitat', or 'exit'.
habitat
Details on penguin habitat
Details on bird house
Details on aquarium
Please type a habitat form the list above (ie. penguin, bird, aquarium)
penguin
Habitat - Penguin
Temperature: Freezing
ALERT: Food source: Fish in water running low
Cleanliness: Passed
What would you like to monitor? Please type 'animal', 'habitat', or 'exit'.
habitat
Details on penguin habitat
Details on bird house
Details on aquarium
Please type a habitat form the list above (ie. penguin, bird, aquarium)
aquarium
Habitat - Aquarium
Temperature: Varies with output temperature
Food source: Added daily
*Cleanliness: Needs cleaning from algae
What would you like to monitor? Please type 'animal', 'habitat', or 'exit'.

You are viewing a single comment. View All
1
heyitsmarcus (285)

Hey @steehs1532,

There was very little I had to change to get this to work and be more dynamic. But, pay close attention. Don't use for loops with static indices when you're reading a file for data. You need a more dynamic approach, in which you identify what separates your data and then while there is data to be read in, do your logic.

You'll notice that I replaced all of your for loops with a while loop. What if you were handed a file with a ton of animals in it, with this setting. You'd have to count each individual block of animals to figure out your for loop. Instead, we use a while loop to dynamically be able to iterate over blocks of data. Since you separated each block of animal or habitat with a newline, we can use that to our advantage.

Here's the repl with the new code: https://repl.it/@heyitsmarcus/project

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;

class Main {
  public static void main(String[] args) throws IOException {
    // initiates a scanner for user input
    Scanner userScan = new Scanner(System.in);

    // initiates string for user input
    String userInput = "";

    // checks for animal/habitat later in the file
    String animalCheck = "";
    String habitatCheck = "";

    // Lets user come back to the options
    while (!userInput.equals("exit")) {
      // input stream for two files
      FileInputStream animalFile = new FileInputStream("animals.txt");
      FileInputStream habitatFile = new FileInputStream("habitats.txt");

      // scanner objects for two files
      // Scanner animalScan = new Scanner(animalFile);
      Scanner habitatScan = new Scanner(habitatFile);
      Scanner animalScan = new Scanner(animalFile);

      System.out.println("What would you like to monitor? Please type 'animal', 'habitat', or 'exit'.");

      // stores user input
      userInput = userScan.nextLine();

      // checks user input
      if (userInput.equals("animal")) {
        // prints the animals
        while (animalScan.hasNextLine()) {
          String line = animalScan.nextLine();
          System.out.println(line);
          if (line.trim().length() < 1) {
            break;
          }
        }
        System.out.println("Please type an animal from the list above (ie. lion, tiger, bear, giraffe)");
        // gets user input
        userInput = userScan.nextLine();

        // cycles through the animals.txt until animal check equals the users animal
        // input
        while (!animalCheck.equalsIgnoreCase(userInput)) {
          animalCheck = animalScan.next();
        }

        // pads the prompt because we skipped over animal - with the scanner
        System.out.println("Animal - " + animalCheck);

        animalScan.nextLine();

        // prints the animal details
        while (animalScan.hasNextLine()) {
          // checks for alerts
          String asteriskCheck = animalScan.nextLine();

          if (asteriskCheck.trim().length() < 1) {
            break;
          }
          
          if (asteriskCheck.contains("*****")) { 
            System.out.print("ALERT: ");
            //removes aterisk
            System.out.println(asteriskCheck.replace("*", "")); 
          } else {
            //prints the line if it wasn't an alert
            System.out.println(asteriskCheck);
          }
           // keeps printing
          //System.out.println(animalScan.nextLine());
        }
        // reset animal file scanner
        animalScan.close();
      } else {
        //prints the habitats
        while (habitatScan.hasNextLine()) {
          String line = habitatScan.nextLine();
          System.out.println(line);
          if (line.trim().length() < 1) {
            break;
          }
        }
        System.out.println("Please type a habitat form the list above (ie. penguin, bird, aquarium)");
        //gets user input
        userInput = userScan.nextLine();

        // cycles through the habitats.txt until animal check equals the users animal
        // input
        while (!habitatCheck.equalsIgnoreCase(userInput)) {
          habitatCheck = habitatScan.next();
        }

        // pads the prompt because we skipped over animal - with the scanner
        System.out.println("Habitat - " + habitatCheck);

        habitatScan.nextLine();

        // prints the habitat details
        while (habitatScan.hasNextLine()) {
          // checks for alerts
          String asteriskCheck = habitatScan.nextLine();
          if (asteriskCheck.trim().length() < 1) {
            break;
          }
          if (asteriskCheck.contains("*****")) { 
            System.out.print("ALERT: ");
            //removes aterisk
            System.out.println(asteriskCheck.replace("*", "")); 
          } else {
            //prints the line if it wasn't an alert
            System.out.println(asteriskCheck);
          }
           // keeps printing
          //System.out.println(habitatScan.nextLine());
        }
        // reset animal file scanner
        habitatScan.close();
      }
    }
  }
}

If this solves your problem, please consider upvoting and marking my answer as the accepted answer. I will continue to answer questions you may have.