Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Every time I try to run the program below, I get the fllowng Error: \"Exception

ID: 3856123 • Letter: E

Question

Every time I try to run the program below, I get the fllowng Error:

"Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at Assignment3.<init>(Assignment3.java:33)

at Assignment3.main(Assignment3.java:10)"

Please assist correct code.

Assigment3.java

import java.util.*;

public class Assignment3

{

private Scanner input;

private Object f;

public static void main(String[] args){

new Assignment3 ();

  

}

  

public Assignment3()

{

input = new Scanner(System.in);

ArrayList<Flower> flowerPack = new ArrayList<Flower>();

  

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: Search for a flower.");

System.out.println("4: 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:

searchFlowers(flowerPack);

break;

case 4:

displayFlowers(flowerPack);

break;

case 0:

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

System.exit(0);

}

System.out.println();

  

}

  

}

private void addFlower(ArrayList<Flower> flowerPack) {

Flower f = new Flower();

Scanner input = new Scanner(System.in);

//get flower name

System.out.print("Enter flower name: ");

f.setName(input.nextLine());

  

//get flower color

System.out.print("Enter flower color: ");

f.setColor(input.nextLine());

  

//thornes ?

System.out.print("Is thorns present (Y/N): ");

String thorns = input.nextLine();

if(thorns == "Y" || thorns == "y"){

f.setThorns(true);

}

else{

f.setThorns(false);

}

  

//get smell

System.out.print("Enter flower smell: ");

f.setSmell(input.nextLine());

  

//add to list

flowerPack.add(f);

  

}

private void removeFlower(ArrayList<Flower> flowerPack) {

System.out.print("Enter name of flower to be romoved: "); // taking user input

String name = input.nextLine();

for(int i=0; i<flowerPack.size(); i++){

if(flowerPack.get(i).getName().equalsIgnoreCase(name)){

flowerPack.remove(f);

break;

  

  

  

  

} }

}

private void searchFlowers(ArrayList<Flower> 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

Scanner input = new Scanner(System.in);

System.out.print("Search a flower in Pack: ");

String flower = input.nextLine();

for (Flower f : flowerPack) {

if (f.getName().contains(flower))

{

System.out.println(f.getName() + " Color: " + f.getColor() + " Throns: " + f.isThorns() + " Smell: " + f.getSmell());

}

}

}

  

private void displayFlowers(ArrayList<Flower> flowerPack) {

// TODO: Display only the unique flowers along with a count of any duplicates

/*

* For example it should say

* Hibiscuss - 7

* Daffodils - 2

* Roses - 5

System.out.println();

System.out.println("List Of Flowers: ");

for (Flower f : flowerPack)

{

int count = 0;

for(int x=0; x<flowerPack.size();x++){

if(f.getName().equals(f.getName())){

count++;

}

}

System.out.println(f.getName() + " - " + count);

}

System.out.println();

}*/

int x = 0;

String[] arrayflower = new String[flowerPack.size()];

for (Flower f : flowerPack)

{

arrayflower[x] = f.getName();

x++;

}

  

int found[] = new int[arrayflower.length];

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

int count = 0;

if (arrayflower[i] == null){

break;

}

  

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

if (arrayflower[j] == null){

break;

}

else if(arrayflower[i].equalsIgnoreCase(arrayflower[j]))

{

count++;

if(i!=j){

found[j] = 1;

}

}

}

  

if(found[i]==0){

System.out.println(arrayflower[i] + " - " + count);

}

}

  

System.out.println();

  

}

}

Flower.java

import java.util.*;

public class Assignment3

{

private Scanner input;

private Object f;

public static void main(String[] args){

new Assignment3 ();

  

}

  

public Assignment3()

{

input = new Scanner(System.in);

ArrayList<Flower> flowerPack = new ArrayList<Flower>();

  

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: Search for a flower.");

System.out.println("4: 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:

searchFlowers(flowerPack);

break;

case 4:

displayFlowers(flowerPack);

break;

case 0:

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

System.exit(0);

}

System.out.println();

  

}

  

}

private void addFlower(ArrayList<Flower> flowerPack) {

Flower f = new Flower();

Scanner input = new Scanner(System.in);

//get flower name

System.out.print("Enter flower name: ");

f.setName(input.nextLine());

  

//get flower color

System.out.print("Enter flower color: ");

f.setColor(input.nextLine());

  

//thornes ?

System.out.print("Is thorns present (Y/N): ");

String thorns = input.nextLine();

if(thorns == "Y" || thorns == "y"){

f.setThorns(true);

}

else{

f.setThorns(false);

}

  

//get smell

System.out.print("Enter flower smell: ");

f.setSmell(input.nextLine());

  

//add to list

flowerPack.add(f);

  

}

private void removeFlower(ArrayList<Flower> flowerPack) {

System.out.print("Enter name of flower to be romoved: "); // taking user input

String name = input.nextLine();

for(int i=0; i<flowerPack.size(); i++){

if(flowerPack.get(i).getName().equalsIgnoreCase(name)){

flowerPack.remove(f);

break;

  

  

  

  

} }

}

private void searchFlowers(ArrayList<Flower> 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

Scanner input = new Scanner(System.in);

System.out.print("Search a flower in Pack: ");

String flower = input.nextLine();

for (Flower f : flowerPack) {

if (f.getName().contains(flower))

{

System.out.println(f.getName() + " Color: " + f.getColor() + " Throns: " + f.isThorns() + " Smell: " + f.getSmell());

}

}

}

  

private void displayFlowers(ArrayList<Flower> flowerPack) {

// TODO: Display only the unique flowers along with a count of any duplicates

/*

* For example it should say

* Hibiscuss - 7

* Daffodils - 2

* Roses - 5

System.out.println();

System.out.println("List Of Flowers: ");

for (Flower f : flowerPack)

{

int count = 0;

for(int x=0; x<flowerPack.size();x++){

if(f.getName().equals(f.getName())){

count++;

}

}

System.out.println(f.getName() + " - " + count);

}

System.out.println();

}*/

int x = 0;

String[] arrayflower = new String[flowerPack.size()];

for (Flower f : flowerPack)

{

arrayflower[x] = f.getName();

x++;

}

  

int found[] = new int[arrayflower.length];

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

int count = 0;

if (arrayflower[i] == null){

break;

}

  

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

if (arrayflower[j] == null){

break;

}

else if(arrayflower[i].equalsIgnoreCase(arrayflower[j]))

{

count++;

if(i!=j){

found[j] = 1;

}

}

}

  

if(found[i]==0){

System.out.println(arrayflower[i] + " - " + count);

}

}

  

System.out.println();

  

}

}

Explanation / Answer

import java.util.*;

public class Assignment3 {

private Scanner input;

private Object f;

public static void main(String[] args) {

new Assignment3();

}

public Assignment3() {

input = new Scanner(System.in);

ArrayList<Flower> flowerPack = new ArrayList<Flower>();

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: Search for a flower.");

System.out.println("4: 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:

searchFlowers(flowerPack);

break;

case 4:

displayFlowers(flowerPack);

break;

case 0:

System.out

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

System.exit(0);

}

System.out.println();

}

}

private void addFlower(ArrayList<Flower> flowerPack) {

Flower f = new Flower();

Scanner input = new Scanner(System.in);

// get flower name

System.out.print("Enter flower name: ");

f.setName(input.nextLine());

// get flower color

System.out.print("Enter flower color: ");

f.setColor(input.nextLine());

// thornes ?

System.out.print("Is thorns present (Y/N): ");

String thorns = input.nextLine();

if (thorns.charAt(0) == 'Y' || thorns.charAt(0) == 'y') {

f.setThorns(true);

} else {

f.setThorns(false);

}

// get smell

System.out.print("Enter flower smell: ");

f.setSmell(input.nextLine());

// add to list

flowerPack.add(f);

}

private void removeFlower(ArrayList<Flower> flowerPack) {

System.out.print("Enter name of flower to be romoved: "); // taking user

// input

String name = input.nextLine();

for (int i = 0; i < flowerPack.size(); i++) {

if (flowerPack.get(i).getName().equalsIgnoreCase(name)) {

flowerPack.remove(f);

break;

}

}

}

private void searchFlowers(ArrayList<Flower> 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

Scanner input = new Scanner(System.in);

System.out.print("Search a flower in Pack: ");

String flower = input.nextLine();

for (Flower f : flowerPack) {

if (f.getName().contains(flower)) {

System.out.println(f.getName() + " Color: " + f.getColor()

+ " Throns: " + f.isThorns() + " Smell: "

+ f.getSmell());

}

}

}

private void displayFlowers(ArrayList<Flower> flowerPack) {

// TODO: Display only the unique flowers along with a count of any

// duplicates

/*

* For example it should say Hibiscuss - 7 Daffodils - 2 Roses - 5

*

* System.out.println(); System.out.println("List Of Flowers: "); for

* (Flower f : flowerPack) { int count = 0; for(int x=0;

* x<flowerPack.size();x++){ if(f.getName().equals(f.getName())){

* count++; } } System.out.println(f.getName() + " - " + count); }

* System.out.println();

*

* }

*/

int x = 0;

String[] arrayflower = new String[flowerPack.size()];

for (Flower f : flowerPack) {

arrayflower[x] = f.getName();

x++;

}

int found[] = new int[arrayflower.length];

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

int count = 0;

if (arrayflower[i] == null) {

break;

}

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

if (arrayflower[j] == null) {

break;

} else if (arrayflower[i].equalsIgnoreCase(arrayflower[j])) {

count++;

if (i != j) {

found[j] = 1;

}

}

}

if (found[i] == 0) {

System.out.println(arrayflower[i] + " - " + count);

}

}

System.out.println();

}

}

public class Flower {

private String name, color, smell;

private boolean thorns;

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @return the color

*/

public String getColor() {

return color;

}

/**

* @return the smell

*/

public String getSmell() {

return smell;

}

/**

* @return the thorns

*/

public boolean isThorns() {

return thorns;

}

/**

* @param name

* the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @param color

* the color to set

*/

public void setColor(String color) {

this.color = color;

}

/**

* @param smell

* the smell to set

*/

public void setSmell(String smell) {

this.smell = smell;

}

/**

* @param thorns

* the thorns to set

*/

public void setThorns(boolean thorns) {

this.thorns = thorns;

}

}

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.
3: Search for a flower.
4: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter flower name: Rose
Enter flower color: Red
Is thorns present (Y/N): y
Enter flower smell: rose

1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: Display the flowers in the pack.
0: Exit the flower pack interfact.
4
Rose - 1


1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: Display the flowers in the pack.
0: Exit the flower pack interfact.
3
Search a flower in Pack: Rose
Rose Color: Red Throns: true Smell: rose

1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: 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!

NOTE: Please check , for what input you are getting the exception , i will explain

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote