My display won\'t work if my pack is full. When it is not full, it seems to be w
ID: 3792680 • Letter: M
Question
My display won't work if my pack is full. When it is not full, it seems to be working fine, but I'm getting an error only when I try to display my pack when it's full.
import java.util.Scanner;
public class FlowerPackAssn1 {
public static void main(String[] args){
new FlowerPackAssn1 ();
}
// This will act as our program switchboard
public FlowerPackAssn1 (){
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++){
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;
System.out.print("Which flower do you want to remove? ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++) {
if(flowerPack[i].equals(flower)) {
flowerPack[i] = null;
System.out.println("Removed!");
break;
}else {
System.out.println("No Match");
break;
}
}
//for(int i = 0; i < flowerPack.length; i++){
// 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++){
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(flowerPack[i] != null && i < flowerPack.length && uniqueFlower.compareTo(flowerPack[i]) == 0) {
counter++;
i++;
}
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
import java.util.*;
import java.util.Scanner;
public class FlowerPackAssn1
{
public static void main(String[] args)
{
new FlowerPackAssn1 ();
}
public FlowerPackAssn1 ()
{
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)
{
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.");
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 6:
System.out.println("Thank you for using the flower pack interface. See you again soon!");
System.exit(0);
}
}
}
private static String[] add(String[] flowerPack,String newvalue)
{
String[] temp=new String[flowerpack.length +1];
for(int i = 0; i < flowerPack.length; i++)
{
temp[i] = flowerpack[i];
}
temp[stringArray.length] =newvalue;
return temp;
System.out.println("");
System.out.println("Please select a number from the options below");
int userChoice = input.nextInt();
}
private void removeFlower(String flowerPack[])
{
Scanner input = new Scanner(System.in);
String flower;
System.out.print("Which flower do you want to remove? ");
flower = input.nextLine();
for(int i = 0; i < flowerPack.length; i++)
{
if(flowerPack[i].equals(stringToRemove))
{
flowerPack[i] = null;
System.out.println("Removed!");
break;
}
else
{
System.out.println("No Match");
break;
}
}
System.out.println("");
System.out.println("Please select a number from the options below");
int userChoice = input.nextInt();
}
private void sortFlowers(String flowerPack[])
{
Arrays.sort(flowerpack);
for(int i = 0; i < flowerPack.length; i++)
{
System.out.println(flowerPack[i]);
}
System.out.println("");
System.out.println("Please select a number from the options below");
int userChoice = input.nextInt();
}
private void searchFlowers(String flowerPack[])
{
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)
{
System.out.println("match found");
}
else
{
System.out.println("Sorry, no match!");
}
System.out.println("");
System.out.println("Please select a number from the options below");
int userChoice = input.nextInt();
}
private void displayFlowers(String flowerPack[])
{
String uniqueFlower;
int counter = 0;
System.out.println("Your flower pack has:");
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(flowerPack[i] != null && i < flowerPack.length && uniqueFlower.compareTo(flowerPack[i]) == 0) {
counter++;
i++;
}
System.out.println(uniqueFlower + " - " + counter);
if(counter > 0) {
i--;
}
}
System.out.println("");
System.out.println("Please select a number from the options below");
int userChoice = input.nextInt();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.