0

Fixed it. Working code! I had to move the fruit array into the method and call it from main.

import java.util.Scanner;

public class StringArrayTest {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String input;       

    System.out.println("Enter a fruit: ");
    input = sc.nextLine();
    System.out.println("Is \"" + input + "\" in the array? " + isIn(input));

    sc.close();
}

public static boolean isIn(String input) {

    String[] fruit = new String[6];

    fruit[0] = "grape";
    fruit[1] = "banana";
    fruit[2] = "apple";
    fruit[3] = "mango";
    fruit[4] = "watermelon";
    fruit[5] = "orange";

    for(int i = 0; i < fruit.length; i++) {
        if (input.equals(fruit[i])) {
            return true;
        }
    }
    return false;
}

}

2
  • 1
    @Reimeus: It's in the main method...that should suffice for now. Commented Feb 25, 2015 at 16:33
  • What's the error you get? Commented Feb 25, 2015 at 16:34

3 Answers 3

4

The following code will return true if the array contains the input and false if it doesn't. Credits to camickr for this code:

Arrays.asList(yourArray).contains(yourString)
Sign up to request clarification or add additional context in comments.

Comments

3

return false must be outside the for loop. If it's in the else part, the for loop will end at the first iteration.

Here's how the code should look like:

public boolean isIn(String input) {
    for(int i = 0; i < fruit.length; i++) {
        if (input.equals(fruit[i])) {
            return true;
        }
    }
    return false;
}

Also, you cannot declare a method inside another method. Move the method isIn outside main.

Other problems in your code:

  • String[] fruit is currently declared, initialized and filled inside main method. It should be at least declared outside as a field of the class in order to be accessed by other methods.
  • You're not using isIn anywhere.
  • In order to use the fields and methods inside main method, you have two options:
    1. Declare the fields and methods as static.
    2. Create a new instance of your class inside the main method and use the fields and methods from this instance.

The code may look like this:

import java.util.Scanner;

public class StringArrayTest {
    static String[] fruit = new String[6];

    public static void main(String[] args) {
        fruit[0] = "grape";
        fruit[1] = "banana";
        fruit[2] = "apple";
        fruit[3] = "mango";
        fruit[4] = "watermelon";
        fruit[5] = "orange";

        Scanner sc = new Scanner(System.in);
        String input;
        input = sc.nextLine();

        if (isIn(input)) {
            //do something...
        } else {
            //do something else...
        }
    }

    public static boolean isIn(String input) {
        for(int i = 0; i < fruit.length; i++) {
            if (input.equals(fruit[i])) {
                return true;
            }
        }
        return false;
    }
}

2 Comments

@user2938819 an error that we don't know since you haven't provided your new code.
isIn doesn't know where fruit is coming from now.
0

Summarizing the suggestions given by others.

import java.util.Arrays;
import java.util.Scanner;

public class StringArrayTest {

   public static void main(String[] args) {

   String[] fruit = new String [] {"grape", "banana", "apple", "mango", "watermelon", "orange"};

   Scanner sc = new Scanner(System.in);
   String input = sc.nextLine();

   System.out.println(Arrays.asList(fruit).contains(input));
 }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.