Tdog Nation
Tdog Nation

Reputation: 13

I am trying to make a for loop with a if statement in it to find a specific name in an arraylist

I have a scanner for input to find a name in an arraylist but when i try to use the .get method it says it can only do that with integers is there any way to get the persons input to == names(i)?

here is my code

import java.util.ArrayList; import java.util.Scanner;

public class Searching {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    ArrayList<String> names = new ArrayList<String>();
    names.add("Liam");
    names.add("Noah");
    names.add("William");
    names.add("James");
    names.add("Logan");
    names.add("Benjamin");
    names.add("Mason");
    names.add("Elijah");



    //Part 1
    System.out.println("Please pick a name:");
       Scanner input = new Scanner(System.in);
        String sentence = input.nextLine();
    int counter = 0;
    for (String i : names)
    {
        if (sentence = names.(i))
        {

        }

        counter++;

    }








}

}

Upvotes: 1

Views: 91

Answers (1)

Mohamed
Mohamed

Reputation: 131

I think what you have to do, is

for (String name : names) //change i to name (for better readability)
    {
        if (name.equals(sentence))
        {

        }

        counter++;

    }

and i is of type String, ArrayList::get method gets an int.

Upvotes: 2

Related Questions