Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

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.(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 flowerPack = new ArrayList();

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 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 flowerPack) {

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

String name = input.nextLine();

for(int i=0; i

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

flowerPack.remove(f);

break;

} }

}

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

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 flowerPack = new ArrayList();

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 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 flowerPack) {

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

String name = input.nextLine();

for(int i=0; i

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

flowerPack.remove(f);

break;

} }

}

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

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();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

Where in the chart of accounts is a suspense account located?

Answered: 1 week ago