I cant get my flowers to display Driver import javautilScann

I can\'t get my flowers to display.

//Driver

import java.util.Scanner;

public class Driver {
   public static void main(String[] args){
       new Driver ();
   }
  
   // This will act as our program switchboard
   public Driver (){
       Scanner input = new Scanner(System.in);
       Flowers[] newFlowers = new Flowers[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(\"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(newFlowers);
                   break;
               case 2:
                   removeFlower(newFlowers);
                   break;
               case 4:
                   searchFlowers(newFlowers);
                   break;
               case 5:
                   displayFlowers(newFlowers);
                   break;
               case 0:
                   System.out.println(\"Thank you for using the flower pack interface. See you again soon!\");
                   System.exit(0);
           }
       }
      
   }

   private void addFlower(Flowers[] newFlowers) {
       // TODO: Add a flower that is specified by the user
       Scanner input = new Scanner(System.in);
       String Name, Color, Thorn, withThorn, Smell;

      
       System.out.print(\"Enter flower Name: \");
       Name = input.nextLine();
       System.out.print(\"Enter flower Colors: \");
       Color = input.nextLine();
      
       for(int i = 0; i < newFlowers.length; i++) {
           if(newFlowers[i] == null) {
               newFlowers[i] = new Flowers(Name, Color);
               System.out.println(\"Added!\");
               break;
              
           } else if(i == newFlowers.length-1 && newFlowers[i] != null) {
               System.out.println(\"Your pack is full!\");
               break;
           }
       }
       //preview
       for(int i = 0; i < newFlowers.length; i++){
           if(newFlowers[i] != null)
           System.out.println(newFlowers[i].toString());
       }
      
       System.out.println(\"\");
       System.out.println(\"Please select a number from the options below\");

   }

   private void removeFlower(Flowers[] newFlowers) {
       // TODO: Remove a flower that is specified by the user
       Scanner input = new Scanner(System.in);
       String RemoveName;
      
       System.out.print(\"Which flower do you want to remove? \");
       RemoveName = input.nextLine();
      
       for(int i = 0; i < newFlowers.length; i++) {
           if((newFlowers[i].getName()).equals(RemoveName)) {
               newFlowers[i] = null;
               System.out.println(\"Removed!\");
               break;

           }
       }
       //preview
       for(int i = 0; i < newFlowers.length; i++){
           if(newFlowers[i] != null)
           System.out.println(newFlowers[i].toString());
       }
      
       System.out.println(\"\");
       System.out.println(\"Please select a number from the options below\");

   }

   private void searchFlowers(Flowers[] newFlowers) {
       // 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 < newFlowers.length; i++) {
           if((newFlowers[i].getName()).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(Flowers[] newFlowers) {
       // 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 < newFlowers.length; i++) {
           for(int j = 0; j < newFlowers.length; j++) {
               if((newFlowers[j].getName()).compareTo((newFlowers[i].getName())) > 0) {
                   Flowers temp = newFlowers[i];
                   newFlowers[i] = newFlowers[j];
                   newFlowers[j] = temp;
               }
           }
       }
      
       for(int i = 0; i < newFlowers.length; i++) {
           counter = 0;
           uniqueFlower = newFlowers[i].getName();
           while(i < newFlowers.length && uniqueFlower.compareTo(newFlowers[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\");
   }
  
}

// Flowers


public class Flowers {

   private String Name;
   private String Color;

  
   public Flowers(String Name, String Color) {
       this.Name = Name;
       this.Color = Color;
      
   }
  
   public Flowers() {
      
   }
  
   public String getName() {
       return Name;
   }
  
   public void setName(String Name) {
       Name = Name;
   }
  
   public String getColor() {
       return Color;
   }
  
   public void setColor(String Color) {
       Color = Color;
   }
  
  
   public String toString() {
       return \" Name: \" + Name + \"\ Color: \" + Color;
   }
  
  
}

Solution

1) There is a complie time error in the display function, at the line:

while(i < newFlowers.length && uniqueFlower.compareTo(newFlowers[i]) == 0) {

From the above line, need to change, newFlowers[i].getName() to compare with uniqueFlower as,

while(i < newFlowers.length && uniqueFlower.compareTo(newFlowers[i].getName()) == 0) {

//Flowers.java

public class Flowers {

private String Name;

private String Color;

  

public Flowers(String Name, String Color) {

this.Name = Name;

this.Color = Color;

  

}

  

public Flowers() {

  

}

  

public String getName() {

return Name;

}

  

public void setName(String Name) {

this.Name = Name;

}

  

public String getColor() {

return Color;

}

  

public void setColor(String Color) {

this.Color = Color;

}

  

  

public String toString() {

return \" Name: \" + Name + \"\ Color: \" + Color;

}

  

  

}

//Driver.java

import java.util.Scanner;

public class Driver {

public static void main(String[] args){

new Driver ();

}

  

// This will act as our program switchboard

public Driver (){

Scanner input = new Scanner(System.in);

Flowers[] newFlowers = new Flowers[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(\"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(newFlowers);

break;

case 2:

removeFlower(newFlowers);

break;

case 4:

searchFlowers(newFlowers);

break;

case 5:

displayFlowers(newFlowers);

break;

case 0:

System.out.println(\"Thank you for using the flower pack interface. See you again soon!\");

System.exit(0);

}

}

  

}

private void addFlower(Flowers[] newFlowers) {

// TODO: Add a flower that is specified by the user

Scanner input = new Scanner(System.in);

String Name, Color, Thorn, withThorn, Smell;

  

System.out.print(\"Enter flower Name: \");

Name = input.nextLine();

System.out.print(\"Enter flower Colors: \");

Color = input.nextLine();

  

for(int i = 0; i < newFlowers.length; i++) {

if(newFlowers[i] == null) {

newFlowers[i] = new Flowers(Name, Color);

System.out.println(\"Added!\");

break;

  

} else if(i == newFlowers.length-1 && newFlowers[i] != null) {

System.out.println(\"Your pack is full!\");

break;

}

}

//preview

for(int i = 0; i < newFlowers.length; i++){

if(newFlowers[i] != null)

System.out.println(newFlowers[i].toString());

}

  

System.out.println(\"\");

System.out.println(\"Please select a number from the options below\");

}

private void removeFlower(Flowers[] newFlowers) {

// TODO: Remove a flower that is specified by the user

Scanner input = new Scanner(System.in);

String RemoveName;

  

System.out.print(\"Which flower do you want to remove? \");

RemoveName = input.nextLine();

  

for(int i = 0; i < newFlowers.length; i++) {

if((newFlowers[i].getName()).equals(RemoveName)) {

newFlowers[i] = null;

System.out.println(\"Removed!\");

break;

}

}

//preview

for(int i = 0; i < newFlowers.length; i++){

if(newFlowers[i] != null)

System.out.println(newFlowers[i].toString());

}

  

System.out.println(\"\");

System.out.println(\"Please select a number from the options below\");

}

private void searchFlowers(Flowers[] newFlowers) {

// 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 < newFlowers.length; i++) {

if((newFlowers[i].getName()).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(Flowers[] newFlowers) {

// 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 < newFlowers.length; i++) {

for(int j = 0; j < newFlowers.length; j++) {

if((newFlowers[j].getName()).compareTo((newFlowers[i].getName())) > 0) {

Flowers temp = newFlowers[i];

newFlowers[i] = newFlowers[j];

newFlowers[j] = temp;

}

}

}

  

for(int i = 0; i < newFlowers.length; i++) {

counter = 0;

uniqueFlower = newFlowers[i].getName();

while(i < newFlowers.length && uniqueFlower.compareTo(newFlowers[i].getName()) == 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\");

}

  

}

Output:

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.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

1

Enter flower Name: Rose

Enter flower Colors: White Red

Added!

Name: Rose

Color: Red

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from the pack.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

1

Enter flower Name: Jasmine

Enter flower Colors: White

Added!

Name: Rose

Color: White

Name: Jasmine

Color: White

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from the pack.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

1

Enter flower Name: Daffodils

Enter flower Colors: Green

Added!

Name: Rose

Color: White

Name: Jasmine

Color: White

Name: Daffodils

Color: Green

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from the pack.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

1

Enter flower Name: Datura

Enter flower Colors: White

Added!

Name: Rose

Color: Red

Name: Jasmine

Color: White

Name: Daffodils

Color: Green

Name: Datura

Color: White

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from the pack.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

1

Enter flower Name: Lilly

Enter flower Colors: White Green

Added!

Name: Rose

Color: White Red

Name: Jasmine

Color: White

Name: Daffodils

Color: Green

Name: Datura

Color: White

Name: Lilly

Color: White Green

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from 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:

Daffodils - 1

Datura - 1

Jasmine - 1

Lilly - 1

Rose - 1

Please select a number from the options below

1: Add an item to the pack.

2: Remove an item from the pack.

4: Search for a flower.

5: Display the flowers in the pack.

0: Exit the flower pack interfact.

I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi
I can\'t get my flowers to display. //Driver import java.util.Scanner; public class Driver { public static void main(String[] args){ new Driver (); } // This wi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site