having trouble with displayFlower. i can\'t get all the flowers to display. keep
ID: 3793292 • Letter: H
Question
having trouble with displayFlower. i can't get all the flowers to display. keep getting error regardless that ppack is full or not.
import java.util.Scanner;
public class FlowerPack {
public static void main(String[] args){
new FlowerPack ();
}
// This will act as our program switchboard
public FlowerPack (){
Scanner input = new Scanner(System.in);
String[] flowerPack = new String[5];
System.out.println("Welcome to my flower pack interface.");
System.out.println("Please select a number from the options below");
System.out.println("");
while(true){
// Give the user a list of their options
System.out.println("1: Add an item to the pack.");
System.out.println("2: Remove an item from the pack.");
System.out.println("3: Sort the contents of the pack.");
System.out.println("4: Search for a flower.");
System.out.println("5: Display the flowers in the pack.");
System.out.println("0: Exit the flower pack interfact.");
// Get the user input
int userChoice = input.nextInt();
switch(userChoice){
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
sortFlowers(flowerPack);
break;
case 4:
searchFlowers(flowerPack);
break;
case 5:
displayFlowers(flowerPack);
break;
case 0:
System.out.println("Thank you for using the flower pack interface. See you again soon!");
System.exit(0);
}
}
}
private void addFlower(String flowerPack[]) {
// TODO: Add a flower that is specified by the user
Scanner input = new Scanner(System.in);
String flower;
System.out.print("Enter flower to add: ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] == null || flowerPack[i] == "null") {
flowerPack[i] = flower;
System.out.println("Added!");
break;
} else if(i == flowerPack.length-1 && (flowerPack[i] != null || flowerPack[i] != "null")) {
System.out.println("Your pack is full!");
break;
}
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
Scanner input = new Scanner(System.in);
String flower;
boolean found = false;
System.out.print("Which flower do you want to remove? ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] != null && flowerPack[i].equals(flower)) {
flowerPack[i] = null;
System.out.println("Removed!");
found = true;
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void sortFlowers(String flowerPack[]) {
// TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
// NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings
for(int i = 0; i < flowerPack.length; i++) {
for(int j = 0; j < flowerPack.length; j++) {
if(flowerPack[i] != null && flowerPack[j] != null &&
flowerPack[j].compareTo(flowerPack[i]) > 0) {
String temp = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = temp;
}
}
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void searchFlowers(String flowerPack[]) {
// TODO: Search for a user specified flower
Scanner input = new Scanner(System.in);
System.out.print("Enter flower name to search: ");
String flower = input.nextLine();
boolean found = false;
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] != null && flowerPack[i].equals(flower)) {
found = true;
System.out.println("Your flower " + flower + " is in position "
+ (i + 1) + " of" + " 5 positions.");
break;
}
}
if(found) {
} else {
System.out.println("Sorry, no match!");
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void displayFlowers(String flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/
String uniqueFlower;
int counter = 0;
System.out.println("Your flower pack has:");
//need to sort flowers first
for(int i = 0; i < flowerPack.length; i++) {
for(int j = 0; j < flowerPack.length; j++) {
if(flowerPack[i] != null && flowerPack[j] != null &&
flowerPack[j].compareTo(flowerPack[i]) > 0) {
String temp = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = temp;
}
}
}
for(int i = 0; i < flowerPack.length; i++) {
counter = 0;
uniqueFlower = flowerPack[i];
while(i < flowerPack.length && uniqueFlower.compareTo(flowerPack[i]) == 0) {
counter++;
i++;
}
if(flowerPack[i]!= null)
System.out.println(uniqueFlower + " - " + counter);
if(counter > 0) {
i--;
}
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
}
Explanation / Answer
HI, I have fixed the issue.
Please let me know in case of any issue.
import java.util.Scanner;
public class FlowerPack {
public static void main(String[] args){
new FlowerPack ();
}
// This will act as our program switchboard
public FlowerPack (){
Scanner input = new Scanner(System.in);
String[] flowerPack = new String[5];
System.out.println("Welcome to my flower pack interface.");
System.out.println("Please select a number from the options below");
System.out.println("");
while(true){
// Give the user a list of their options
System.out.println("1: Add an item to the pack.");
System.out.println("2: Remove an item from the pack.");
System.out.println("3: Sort the contents of the pack.");
System.out.println("4: Search for a flower.");
System.out.println("5: Display the flowers in the pack.");
System.out.println("0: Exit the flower pack interfact.");
// Get the user input
int userChoice = input.nextInt();
switch(userChoice){
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
sortFlowers(flowerPack);
break;
case 4:
searchFlowers(flowerPack);
break;
case 5:
displayFlowers(flowerPack);
break;
case 0:
System.out.println("Thank you for using the flower pack interface. See you again soon!");
System.exit(0);
}
}
}
private void addFlower(String flowerPack[]) {
// TODO: Add a flower that is specified by the user
Scanner input = new Scanner(System.in);
String flower;
System.out.print("Enter flower to add: ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] == null || flowerPack[i] == "null") {
flowerPack[i] = flower;
System.out.println("Added!");
break;
} else if(i == flowerPack.length-1 && (flowerPack[i] != null || flowerPack[i] != "null")) {
System.out.println("Your pack is full!");
break;
}
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
Scanner input = new Scanner(System.in);
String flower;
boolean found = false;
System.out.print("Which flower do you want to remove? ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] != null && flowerPack[i].equals(flower)) {
flowerPack[i] = null;
System.out.println("Removed!");
found = true;
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void sortFlowers(String flowerPack[]) {
// TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
// NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings
for(int i = 0; i < flowerPack.length-1; i++) {
for(int j = i+1; j < flowerPack.length; j++) {
if(flowerPack[i] != null && flowerPack[j] != null &&
flowerPack[j].compareTo(flowerPack[i]) > 0) {
String temp = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = temp;
}
}
}
for(int i = 0; i < flowerPack.length; i++){
if(flowerPack[i]!= null)
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void searchFlowers(String flowerPack[]) {
// TODO: Search for a user specified flower
Scanner input = new Scanner(System.in);
System.out.print("Enter flower name to search: ");
String flower = input.nextLine();
boolean found = false;
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i] != null && flowerPack[i].equals(flower)) {
found = true;
System.out.println("Your flower " + flower + " is in position "
+ (i + 1) + " of" + " 5 positions.");
break;
}
}
if(found) {
} else {
System.out.println("Sorry, no match!");
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
private void displayFlowers(String flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/
String uniqueFlower;
int counter = 0;
System.out.println("Your flower pack has:");
//need to sort flowers first
for(int i = 0; i < flowerPack.length-1; i++) {
for(int j = i+1; j < flowerPack.length; j++) {
if(flowerPack[i] != null && flowerPack[j] != null &&
flowerPack[j].compareTo(flowerPack[i]) > 0) {
String temp = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = temp;
}
}
}
for(int i = 0; i < flowerPack.length; ) {
counter = 1;
uniqueFlower = flowerPack[i];
if(uniqueFlower == null){
break;
}
while((i+1) < flowerPack.length && flowerPack[i+1]!=null &&
uniqueFlower.compareTo(flowerPack[i+1]) == 0) {
counter++;
i++;
}
if(flowerPack[i]!= null)
System.out.println(uniqueFlower + " - " + counter);
i++;
}
System.out.println("");
System.out.println("Please select a number from the options below");
}
}
/*
Sample run:
Welcome to my flower pack interface.
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter flower to add: ww
Added!
ww
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Your flower pack has:
ww - 1
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter flower to add: rr
Added!
ww
rr
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Your flower pack has:
ww - 1
rr - 1
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter flower to add: ww
Added!
ww
rr
ww
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Your flower pack has:
ww - 2
rr - 1
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
3
ww
ww
rr
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Your flower pack has:
ww - 2
rr - 1
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
4
Enter flower name to search: ww
Your flower ww is in position 1 of 5 positions.
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
0
Thank you for using the flower pack interface. See you again soon!
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.