Java - when I filter it always shows \"No Match!\" at the beginning even though
ID: 3812875 • Letter: J
Question
Java - when I filter it always shows "No Match!" at the beginning even though filter came back with results.
//Driver.java
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
public class Driver {
public static void main(String[] args) throws Exception {
new Driver();
}
public Driver() throws Exception {
Scanner input = new Scanner(System.in);
Plant newPlant = new Plant();
ArrayList<Plant> plantPack = new ArrayList<Plant>();
System.out.println("Welcome to my PlantPack Interface");
System.out.println("Please select a number from the options below:");
System.out.println("");
while(true) {
System.out.println("1: Add a Plant to the pack.");
System.out.println("2: Remove a Plant from the pack.");
System.out.println("3: Search for a Plant by Name.");
System.out.println("4: Filter PlantPack by Plant Partial Name.");
System.out.println("5: Display Plants in the PlantPack.");
System.out.println("6: Save PlantPack to a File.");
System.out.println("7: Load PlantPack from a File.");
System.out.println("0: Exit the Plant Pack interface.");
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
System.out.println("Enter Plant Type: ");
System.out.println("1: Flower");
System.out.println("2: Fungus");
System.out.println("3: Weed");
int plantType = input.nextInt();
switch(plantType) {
case 1:
newPlant = new Flower();
addPlant(plantPack, newPlant);
break;
case 2:
newPlant = new Fungus();
addPlant(plantPack, newPlant);
break;
case 3:
newPlant = new Weed();
addPlant(plantPack, newPlant);
break;
}
break;
case 2:
removePlant(plantPack);
break;
case 3:
searchPlant(plantPack);
break;
case 4:
filterPlant(plantPack);
break;
case 5:
displayPlant(plantPack);
break;
case 6:
savePlant(plantPack);
break;
case 7:
loadPlant(plantPack);
break;
case 0:
System.out.println("Thank you for using the Plant Pack Interface. See you again soon!");
System.exit(0);
}
}
}
private void savePlant(ArrayList<Plant>plantPack) throws Exception {
try (
java.io.PrintWriter output = new java.io.PrintWriter("PlantPack.txt"); ) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
output.println(" Flower: ");
} else if(plant instanceof Fungus) {
output.println(" Fungus: ");
} else if(plant instanceof Weed) {
output.println(" Weed: ");
}
output.println(plant);
}
}
}
System.out.println("PlantPack Saved!");
System.out.println("");
}
private void loadPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String loadFile;
System.out.println("Enter file name including file format (ex. file.txt): ");
loadFile = input.nextLine();
try {
java.io.File file = new java.io.File(loadFile);
Scanner reader = new Scanner(file);
reader.useDelimiter(" ");
while(reader.hasNext()) {
System.out.println(reader.nextLine());
}
reader.close();
} catch(FileNotFoundException ex) {
System.out.println("File not found.");
}
}
private void addPlant(ArrayList<Plant>plantPack, Plant newPlant) {
Scanner input = new Scanner(System.in);
String Name, ID, Color, Smell, Thorns, Poisonous, Edible, Medicinal;
if(newPlant instanceof Flower) {
System.out.print("Enter Flower ID: ");
ID = input.nextLine();
System.out.print("Enter Flower Name: ");
Name = input.nextLine();
System.out.print("Enter Flower Color: ");
Color = input.nextLine();
System.out.print("How does this Flower smell? ");
Smell = input.nextLine();
System.out.print("Does this Flower have Thorns?(Yes or No): ");
Thorns = input.nextLine();
while(!Thorns.equalsIgnoreCase("Yes") && !Thorns.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Thorns = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Flower)newPlant).setColor(Color);
((Flower)newPlant).setSmell(Smell);
((Flower)newPlant).setThorns(Thorns);
plantPack.add(newPlant);
System.out.println("Flower Added!");
System.out.println("");
} else if(newPlant instanceof Fungus) {
System.out.print("Enter Fungus ID: ");
ID = input.nextLine();
//input.nextLine();
System.out.print("Enter Fungus Name: ");
Name = input.nextLine();
System.out.print("Enter Fungus Color: ");
Color = input.nextLine();
System.out.print("Is this Fungus Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Fungus)newPlant).setColor(Color);
((Fungus)newPlant).setPoisonous(Poisonous);
plantPack.add(newPlant);
System.out.println("Fungus Added!");
System.out.println("");
} else {
System.out.print("Enter Weed ID: ");
ID = input.nextLine();
System.out.print("Enter Weed Name: ");
Name = input.nextLine();
System.out.print("Enter Weed Color: ");
Color = input.nextLine();
System.out.print("Is this Weed Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
System.out.print("Is this Weed Edible?(Yes or No): ");
Edible = input.nextLine();
while(!Edible.equalsIgnoreCase("Yes") && !Edible.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Edible = input.nextLine();
}
System.out.print("Is this Weed Medicinal?(Yes or No): ");
Medicinal = input.nextLine();
while(!Medicinal.equalsIgnoreCase("Yes") && !Medicinal.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Medicinal = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Weed)newPlant).setColor(Color);
((Weed)newPlant).setPoisonous(Poisonous);
((Weed)newPlant).setEdible(Edible);
((Weed)newPlant).setMedicinal(Medicinal);
plantPack.add(newPlant);
System.out.println("Weed Added!");
System.out.println("");
}
}
private void removePlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String removePlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to remove: ");
removePlant = input.nextLine();
for(Plant plant : plantPack) {
if(plant.getName().equalsIgnoreCase(removePlant)) {
plantPack.remove(plant);
System.out.println("Removed " + removePlant + "!");
System.out.println("");
found = true;
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
System.out.println("");
}
}
}
private void searchPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String searchPlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to search: ");
searchPlant = input.nextLine();
for(int i = 0; i < plantPack.size(); i++) {
if(plantPack.get(i).getName().equalsIgnoreCase(searchPlant)) {
found = true;
System.out.println("Your plant " + searchPlant + " is in position "
+ (i + 1));
System.out.println("");
break;
}
}
if(!found) {
System.out.println("No Match!");
System.out.println("");
}
}
}
private void filterPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
System.out.print("Enter partial name of plant to search: ");
String filterPlant = input.nextLine();
ArrayList<Plant> filterPack = new ArrayList<>();
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(int i = 0; i < plantPack.size(); i++) {
Pattern pattern = Pattern.compile(filterPlant, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(plantPack.get(i).getName());
while(matcher.find()) {
filterPack.add(plantPack.get(i));
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
System.out.println("");
}
}
System.out.println("Plants containing " + filterPlant + " are:");
for(Plant plant: filterPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if (plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if (plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
private void displayPlant(ArrayList<Plant>plantPack) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if(plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if(plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
}
}
//Plant.java
public class Plant {
private String Name;
private String ID;
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String toString() {
return " Name: " + Name + " ID: " + ID;
}
}
//Weed.java
public class Weed extends Plant {
private String Color;
private String Poisonous;
private String Edible;
private String Medicinal;
public Weed() {
}
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public String getPoisonous() {
return Poisonous;
}
public void setPoisonous(String Poisonous) {
this.Poisonous = Poisonous;
}
public String getEdible() {
return Edible;
}
public void setEdible(String Edible) {
this.Edible = Edible;
}
public String getMedicinal() {
return Medicinal;
}
public void setMedicinal(String Medicinal) {
this.Medicinal = Medicinal;
}
public String toString() {
return super.toString() + " Color: " + Color + " Poisonous: " + Poisonous + " Edible:" +
Edible + " Medicinal: " + Medicinal;
}
}
//Fungus.java
public class Fungus extends Plant {
private String Color;
private String Poisonous;
public Fungus() {
}
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public String getPoisonous() {
return Poisonous;
}
public void setPoisonous(String Poisonous) {
this.Poisonous = Poisonous;
}
public String toString() {
return super.toString() + " Color: " + Color + " Poisonous: " + Poisonous;
}
}
//Flower.java
public class Flower extends Plant {
private String Color;
private String Smell;
private String Thorns;
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public String getSmell() {
return Smell;
}
public void setSmell(String Smell) {
this.Smell = Smell;
}
public String getThorns() {
return Thorns;
}
public void setThorns(String Thorns) {
this.Thorns = Thorns;
}
public void Name() {
super.getName();
}
public void ID() {
super.getID();
}
public String toString() {
return super.toString() + " Color: " + Color + " Smell: " + Smell + " withThorns: " + Thorns;
}
}
Explanation / Answer
The only problem with your code is that, you forgot to update the boolean variable found in the filterPlant method, when a match is found. Once you do that, it works absolutely fine with....
Here is the modification for the demo class:
//Driver.java
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
public class Driver {
public static void main(String[] args) throws Exception {
new Driver();
}
public Driver() throws Exception {
Scanner input = new Scanner(System.in);
Plant newPlant = new Plant();
ArrayList<Plant> plantPack = new ArrayList<Plant>();
System.out.println("Welcome to my PlantPack Interface");
System.out.println("Please select a number from the options below:");
System.out.println("");
while(true) {
System.out.println("1: Add a Plant to the pack.");
System.out.println("2: Remove a Plant from the pack.");
System.out.println("3: Search for a Plant by Name.");
System.out.println("4: Filter PlantPack by Plant Partial Name.");
System.out.println("5: Display Plants in the PlantPack.");
System.out.println("6: Save PlantPack to a File.");
System.out.println("7: Load PlantPack from a File.");
System.out.println("0: Exit the Plant Pack interface.");
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
System.out.println("Enter Plant Type: ");
System.out.println("1: Flower");
System.out.println("2: Fungus");
System.out.println("3: Weed");
int plantType = input.nextInt();
switch(plantType) {
case 1:
newPlant = new Flower();
addPlant(plantPack, newPlant);
break;
case 2:
newPlant = new Fungus();
addPlant(plantPack, newPlant);
break;
case 3:
newPlant = new Weed();
addPlant(plantPack, newPlant);
break;
}
break;
case 2:
removePlant(plantPack);
break;
case 3:
searchPlant(plantPack);
break;
case 4:
filterPlant(plantPack);
break;
case 5:
displayPlant(plantPack);
break;
case 6:
savePlant(plantPack);
break;
case 7:
loadPlant(plantPack);
break;
case 0:
System.out.println("Thank you for using the Plant Pack Interface. See you again soon!");
System.exit(0);
}
}
}
private void savePlant(ArrayList<Plant>plantPack) throws Exception {
try (
java.io.PrintWriter output = new java.io.PrintWriter("PlantPack.txt"); ) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
output.println(" Flower: ");
} else if(plant instanceof Fungus) {
output.println(" Fungus: ");
} else if(plant instanceof Weed) {
output.println(" Weed: ");
}
output.println(plant);
}
}
}
System.out.println("PlantPack Saved!");
System.out.println("");
}
private void loadPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String loadFile;
System.out.println("Enter file name including file format (ex. file.txt): ");
loadFile = input.nextLine();
try {
java.io.File file = new java.io.File(loadFile);
Scanner reader = new Scanner(file);
reader.useDelimiter(" ");
while(reader.hasNext()) {
System.out.println(reader.nextLine());
}
reader.close();
} catch(FileNotFoundException ex) {
System.out.println("File not found.");
}
}
private void addPlant(ArrayList<Plant>plantPack, Plant newPlant) {
Scanner input = new Scanner(System.in);
String Name, ID, Color, Smell, Thorns, Poisonous, Edible, Medicinal;
if(newPlant instanceof Flower) {
System.out.print("Enter Flower ID: ");
ID = input.nextLine();
System.out.print("Enter Flower Name: ");
Name = input.nextLine();
System.out.print("Enter Flower Color: ");
Color = input.nextLine();
System.out.print("How does this Flower smell? ");
Smell = input.nextLine();
System.out.print("Does this Flower have Thorns?(Yes or No): ");
Thorns = input.nextLine();
while(!Thorns.equalsIgnoreCase("Yes") && !Thorns.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Thorns = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Flower)newPlant).setColor(Color);
((Flower)newPlant).setSmell(Smell);
((Flower)newPlant).setThorns(Thorns);
plantPack.add(newPlant);
System.out.println("Flower Added!");
System.out.println("");
} else if(newPlant instanceof Fungus) {
System.out.print("Enter Fungus ID: ");
ID = input.nextLine();
//input.nextLine();
System.out.print("Enter Fungus Name: ");
Name = input.nextLine();
System.out.print("Enter Fungus Color: ");
Color = input.nextLine();
System.out.print("Is this Fungus Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Fungus)newPlant).setColor(Color);
((Fungus)newPlant).setPoisonous(Poisonous);
plantPack.add(newPlant);
System.out.println("Fungus Added!");
System.out.println("");
} else {
System.out.print("Enter Weed ID: ");
ID = input.nextLine();
System.out.print("Enter Weed Name: ");
Name = input.nextLine();
System.out.print("Enter Weed Color: ");
Color = input.nextLine();
System.out.print("Is this Weed Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
System.out.print("Is this Weed Edible?(Yes or No): ");
Edible = input.nextLine();
while(!Edible.equalsIgnoreCase("Yes") && !Edible.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Edible = input.nextLine();
}
System.out.print("Is this Weed Medicinal?(Yes or No): ");
Medicinal = input.nextLine();
while(!Medicinal.equalsIgnoreCase("Yes") && !Medicinal.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Medicinal = input.nextLine();
}
newPlant.setID(ID);
newPlant.setName(Name);
((Weed)newPlant).setColor(Color);
((Weed)newPlant).setPoisonous(Poisonous);
((Weed)newPlant).setEdible(Edible);
((Weed)newPlant).setMedicinal(Medicinal);
plantPack.add(newPlant);
System.out.println("Weed Added!");
System.out.println("");
}
}
private void removePlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String removePlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to remove: ");
removePlant = input.nextLine();
for(Plant plant : plantPack) {
if(plant.getName().equalsIgnoreCase(removePlant)) {
plantPack.remove(plant);
System.out.println("Removed " + removePlant + "!");
System.out.println("");
found = true;
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
System.out.println("");
}
}
}
private void searchPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String searchPlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to search: ");
searchPlant = input.nextLine();
for(int i = 0; i < plantPack.size(); i++) {
if(plantPack.get(i).getName().equalsIgnoreCase(searchPlant)) {
found = true;
System.out.println("Your plant " + searchPlant + " is in position "
+ (i + 1));
System.out.println("");
break;
}
}
if(!found) {
System.out.println("No Match!");
System.out.println("");
}
}
}
private void filterPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
System.out.print("Enter partial name of plant to search: ");
String filterPlant = input.nextLine();
ArrayList<Plant> filterPack = new ArrayList<>();
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(int i = 0; i < plantPack.size(); i++) {
Pattern pattern = Pattern.compile(filterPlant, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(plantPack.get(i).getName());
while(matcher.find()) {
filterPack.add(plantPack.get(i));
found = true; //This is where your code goes wrong. You forgot to write this step.
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
System.out.println("");
}
}
System.out.println("Plants containing " + filterPlant + " are:");
for(Plant plant: filterPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if (plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if (plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
private void displayPlant(ArrayList<Plant>plantPack) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if(plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if(plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.