Home > front end >  Using scanner input in enhanced For loop to find indexOf an object in ArrayList
Using scanner input in enhanced For loop to find indexOf an object in ArrayList

Time:10-14

I'm trying to take my Scanner input and use it to find the index of the location of a object/name in an ArrayList of Objects. The code is made up of two Classes, constructor(setter/getter) and tester class The array list was created using the following code as example; List<Detail> details = new ArrayList<>();

details.add(new Detail("Anthony", 26) );; note i used public void setName(String name) and public void setNumber(long number) to identify the objects added to the ArrayList

The arraylist output looks like this

Name: Anthony   Age: 26
Name: Thomas    Age: 30
Name: Shaun Age: 29
Name: James Age: 28

The code below is what i'm trying to use to find the index location. This code wont compile because i dont know what to put in the parenthesis of details.indexOf())

System.out.print("Type in one of the names listed above to find index of it's location: ");
   String name = s.nextLine();
   for (Detail d : details){
    if (details.contains(s.nextLine()))
     System.out.println("The index location of "  (scanner input/name here) " is "   details.indexOf());

My intended output is

The index location of Thomas is 1

I Know how to get the index of an element whenit's defined into the code, id use something like int location = details.get(2);, i believe this would return Name: Shaun Age: 29, but i dont know how to take the input from the Scanner Shaun and return only the Location of 2

Am I going about this in silly way? What am I doing wrong?

CodePudding user response:

You can use the below approach to find the index of the name in the list.

Here,

  1. I have iterated over the list and check the input with the names from the list, once found , assign that index and break from the loop.

  2. If user input not found, then user will be notified with the message "name not found".

Code:

With For loop

public class Test {
    public static void main(String[] args) {
        List<Detail> list = Arrays.asList(new Detail("Anthony",26),
                new Detail("Thomas",30),
                new Detail("Shaun",29),
                new Detail("James",28));
        System.out.print("Type in one of the names listed above to find index of it's location: ");
        Scanner s = new Scanner(System.in);
        String name = s.nextLine();
        int index = -1;

        for(int i = 0 ;  i < list.size(); i  ){
            if(name.equals(list.get(i).getName())){
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("name not found");
        } else {
            System.out.println("The index location of "   name   " is "   index);
        }
    }
}

With enhanced For loop

public class Test {
    public static void main(String[] args) {
        List<Detail> list = Arrays.asList(new Detail("Anthony",26),
                new Detail("Thomas",30),
                new Detail("Shaun",29),
                new Detail("James",28));
        System.out.print("Type in one of the names listed above to find index of it's location: ");
        Scanner s = new Scanner(System.in);
        String name = s.nextLine();
        int index = -1;
        int counter = 0;
        for(Detail d : list){
            if(name.equals(d.getName())){
                index = counter;
                break;
            }
            counter  ;
        }
        if(index == -1) {
            System.out.println("name not found");
        } else {
            System.out.println("The index location of "   name   " is "   index);
        }
    }
}

Output::

        Input:: Thomas
        Output:: The index location of Thomas is 1
        
        Input:: Test
        Output:: name not found
  •  Tags:  
  • java
  • Related