Sunday, 8 September 2013

Array- variable cannot be resolved

Array- variable cannot be resolved

The following is code I've written to try to accomplish the following tasks:
have the user enter up to 25 flowers
allow the user to remove one of the flowers
sort the flowers alphabetically
search for a specific flower
display all of the flowers with a value of how many of each type of
flowers are in the array
exit the program
There have been several errors. I looked up how to remove an element from
an array, and it involves the second import as well as the removeItem.
However, eclipse does not recognize the import
(org.apache.commons.lang.ArrayUtils); is there a better way? Or did I type
if wrong. Next it is having problems connecting case 2 through case 5 to
the methods I have created, and I'm not sure why. The most prevalent error
I'm seeing is that n cannot be resolved. I have tried many different ways
of defining n or showing that it represents the index, but nothing I'm
doing is solving the issue. This error occurs at the lines: the first
three lines after the bracket enclosing the addFlower method, under the
removeFlower method at 'if (n <= 0)' and 'if removedItem != flowerPack[n]'
as well as input cannot be resolved at the line above that 'String
removedItem = input.nextLine();', and the ArrayUtils cannot be resolved at
the second else statement under removeFlower,input cannot be resolved when
I try to define what the user inputs under searchFlower at 'String
searchWord = input.nextLine();', n cannot be resolved under
countFlowerTypes 'counts [flowerPack[n]]++;', and there are several errors
on the line 'public static int[] countFlowerTypes(String[] flowerPack){'
including they want ( and ) to be ;, they say it can only be final, and
they call it a duplicate local variable. I know I have many problems, but
I've been trying to look stuff up the last couple of hours and everything
I try just makes it worse. If anyone has any pointers on how to change
these errors that would be extremely helpful. Thanks!
import java.util.Scanner;
import org.apache.commons.lang.ArrayUtils;
public class Assignment1 {
public static void main(String[] args) {
new Assignment1();
}
// This will act as our program switchboard
public Assignment1(){
Scanner input = new Scanner(System.in);
String[]flowerPack = new String[25];
System.out.println("Welcome to my flower pack interface.");
System.out.println("Please select a number from the option 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 interface.");
// Get 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[]){
for (n=0; n < flowerPack.length; n++);
flowerPack[n] = input.nextLine();
if(n >= flowerPack.length)
System.out.println("You have reached your max of 25 flowers.
Delete one if you would like to add another.");
else
System.out.println("Your flower has been added");
}
private void removeFlower(String flowerPack[]){
if (n <= 0)
System.out.println("You have no flowers to remove.");
else
System.out.println("Which flower would you like to remove?");
String removedItem = input.nextLine();
if removedItem != flowerPack[n]
System.out.println("Your entry has not been entered. This
is case sensitive.");
else
flowerPack[] = ArrayUtils.removeElement(flowerPack, removedItem);
}
private void sortFlowers(String flowerPack[]){
for (int n = 0; n < flowerPack.length-1; n++){
String currentMin = flowerPack[n];
int currentMinIndex = n;
for (int j = n + 1; j < flowerPack.length; j++){
if (currentMin.compareTo(flowerPack[j]) > 0){
currentMin = flowerPack[j];
currentMinIndex = j;
}
}}
}
private void searchFlowers(String flowerPack[]){
System.out.println("What is all or part of the flower name you would
like to search for?");
String searchWord = input.nextLine();
for (String n: flowerPack)
if (n.contains(searchWord))
System.out.println("Your flower is" + n);
else
System.out.println("Your search was not found.");
}
private void displayFlowers(String flowerPack[]){
for (int n = 0; n < flowerPack.length; n ++){
System.out.println(flowerPack[n]);
}
public static int[] countFlowerTypes(String[] flowerPack){
int[] counts = new int [25];
for (int n = 0; n < flowerPack.length; n++);
counts [flowerPack[n]]++;
}
}
}

No comments:

Post a Comment